加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react监听input框里的值

发布时间:2020-12-15 16:20:30 所属栏目:百科 来源:网络整理
导读:import React,{ Component } from ‘react‘ class App extends Component { constructor(props) { super(props) // this.state = {} 定义数据 this .state = { inputValue: "" } } render() { return ( div { /* react里使用表达式,需要用大括号 */ } { /*
import React,{ Component } from ‘react‘
class App extends Component {
  constructor(props) {
    super(props)
    //this.state = {}  定义数据
    this.state = {
      inputValue: ""
    }
  }
  render() {
    return (
      <div>
        {/* react里使用表达式,需要用大括号 */}
        {/* react里绑定事件用驼峰命名,如,onChange */}
        {/* react里需要改变this的指向,用bind(this) 把this指向到这个标签里 */}
        <input type="text" value={this.state.inputValue} onChange={this.change.bind(this)} />
      </div>
    )
  }

  change(e) {
    console.log(e.target.value);
    //设置数据的值,用this.setState({})
    this.setState({
      inputValue: e.target.value
    })
  }
}
export default App;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读