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

  • 相关阅读:
    javascript 模板系统 ejs v2
    三国观后感
    《非诚勿扰》乐嘉老师送给男生女生的话:
    正则学习笔记6
    硬链接和符号链接
    javascript 模板系统 ejs v1
    javascript模板系统 ejs v3
    程序员编程艺术:第八章、从头至尾漫谈虚函数
    程序员编程艺术第十一章:最长公共子序列(LCS)问题
    编程艺术第二十三~四章&十一续:杨氏矩阵查找,倒排索引关键词Hash编码
  • 原文地址:https://www.cnblogs.com/laojun/p/6117861.html
Copyright © 2011-2022 走看看