zoukankan      html  css  js  c++  java
  • react中的ref在input中的详解

    当我们在项目中遇见文本输入框的时候,获取时刻输入框中的值

    1、受控组件

    class NameForm extends React.Component {
      constructor(props) {
        super(props);
        this.state = {value: ''};
      }
     render() {
        return (
              <input type="text" value={this.state.value} onChange={this.handleChange} />
        );
      }
       handleChange(event) {
        this.setState({value: event.target.value});
      }
    }

    2、非受控组件

    class NameForm extends React.Component {
      constructor(props) {
        super(props);
        this.state = {value: ''};
      }
     render() {
        return (
              <input 
              type="text"
              ref={el=>this.input =el}
               placeholder="演出/艺人/场馆"//输入框中默认的value
           />
          <button
              onClick={
                 this.Searchtitle.bind(this)
              }
          ></button>
        );
      }
      Searchtitle(){
        console.log(this.input.value) //实时的获取输入框中的值
      }
    }
  • 相关阅读:
    Treap
    P1650 田忌赛马
    wqs二分
    P3810 【模板】三维偏序(陌上花开)(CDQ分治)
    UVA1205 Color a Tree
    P2887 [USACO07NOV]Sunscreen G
    Snowflake Snow Snowflakes
    P1613 跑路
    P5018 [NOIP2018 普及组] 对称二叉树
    装模作样的停课记录
  • 原文地址:https://www.cnblogs.com/houjl/p/10116887.html
Copyright © 2011-2022 走看看