react 获取input的值 ref 和 this.setState({})

对着背影说爱祢 提交于 2020-02-01 17:28:24
1、ref
  //class
  my_filter(reg){
          const inpVal = this.input.value;
          console.log(inpVal);
     console.log(reg);

      };
 
  //render
  <input  type="text" ref={input => this.input = input}  defaultValue = 'helloworld!' />
  <button onClick = {this.my_filter.bind(this, reg)}> 确定 </button>
 
 
2、this.setState({})

  //class

  this.state = {

    inpVal : 'helloworld'

  };

  

  my_filter(e){
          this.setState({ inpValu:e.target.value })
     };
 

  //render

  <input  type="text"  onChange={this.my_filter.bind(this)} defaultValue={this.state.inpValu}/>

 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!