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


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}/>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM