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属性的值

  • 相关阅读:
    telnet连接ip
    完成端口————留着看
    mysql设置最大连接数量
    前端页面唯一字符串生成(Js)UUID
    java加密MD5实现及密码验证
    cookie中存取中文字符
    自定义标签——带标签体
    简单讨论数据类型(byte)强制转化后的数值变化规律
    般若波罗蜜多心经(转载)
    table 表头固定
  • 原文地址:https://www.cnblogs.com/laojun/p/6117861.html
Copyright © 2011-2022 走看看