首页 > 其他 > 详细

react 获取 input 的值

时间:2018-06-22 23:25:56      阅读:323      评论:0      收藏:0      [点我收藏+]

1.通过 onChange -- e.target.value

class App extends Component {
  state = {
    username: ‘张三‘
  };
  // 用户名
  getUserName(e){
    console.log(e.target.value);
    this.setState({
      username: e.target.value
    });
    console.log(this.state.username); // setState为异步,存在延迟
  }

  render() {
    return (
      <div>
        <input type="text" onChange={this.getUserName.bind(this)} />
      </div>
    );
  }
}

2.通过 ref -- this.refs.name

<input type="text" ref="username" />

console.log(this.refs.username.value);

注:最新版本已抛弃此用法?

react 获取 input 的值

原文:https://www.cnblogs.com/crazycode2/p/9202145.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!