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

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) //实时的获取输入框中的值
  }
}

 

相关文章:

  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-27
  • 2022-12-23
  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案