• React之Froms


    In React, a <textarea> uses a value attribute instead. This way, a form using a <textarea>can be written very similarly to a form that uses a single-line input:

    React中    使用value属性显示区域内容。

    <textarea value={this.state.value} onChange={this.handleChange} />

    Note that the Coconut option is initially selected, because of the selected attribute. React, instead of using this selected attribute, uses a value attribute on the root select tag. This is more convenient in a controlled component because you only need to update it in one place. For example:

    hmtl和react中使用select的区别

    html中使用子元素中的selected属性标注

    <select>
      <option value="grapefruit">Grapefruit</option>
      <option value="lime">Lime</option>
      <option selected value="coconut">Coconut</option>
      <option value="mango">Mango</option>
    </select>

    react中在父元素中加入value,然后在通过change时间,修改state中的value,

    class FlavorForm extends React.Component {
      constructor(props) {
        super(props);
        this.state = {value: 'coconut'};
    
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      }
    
      handleChange(event) {
        this.setState({value: event.target.value});
      }
    
      handleSubmit(event) {
        alert('Your favorite flavor is: ' + this.state.value);
        event.preventDefault();
      }
    
      render() {
        return (
          <form onSubmit={this.handleSubmit}>
            <label>
              Pick your favorite La Croix flavor:
              <select value={this.state.value} onChange={this.handleChange}>
                <option value="grapefruit">Grapefruit</option>
                <option value="lime">Lime</option>
                <option value="coconut">Coconut</option>
                <option value="mango">Mango</option>
              </select>
            </label>
            <input type="submit" value="Submit" />
          </form>
        );
      }
    }

    如此一来,react中对input/select、textarea的取值统一为取value属性的值

  • 相关阅读:
    jsp引擎是什么
    asp.net MVC遇到的问题
    java中设置http响应头控制浏览器禁止缓存当前文档内容
    java中文件下载的思路(参考:孤傲苍狼)
    301与302页面重定向
    怎么在后台修改前台html页面的key、title、description
    进程外session(session保存在sqlserver)
    Cookie的读写
    HTTP 方法:GET 对比 POST 转自w3school
    位bit,字节byte,K,M,G(转)
  • 原文地址:https://www.cnblogs.com/laojun/p/6117861.html
Copyright © 2020-2023  润新知