zoukankan      html  css  js  c++  java
  • 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属性的值

  • 相关阅读:
    Apache Hadoop 3.0.0 Release Notes
    控制你的数据,你才能得到有效且高效的数据结果
    读写分离与主从同步数据一致性
    代理ip proxy
    maximize_window fullscreen_window minimize_window
    HTTP 代理原理及实现
    browser user agent
    res_d_l =[{'contents':d.contents,'href':d.attrs['href']} for d in rd] 泛型
    tmp
    Connection reset by peer
  • 原文地址:https://www.cnblogs.com/laojun/p/6117861.html
Copyright © 2011-2022 走看看