在 React 中使用 onChange 时,未注册空格

Dav*_*kee 5 javascript onchange reactjs

我试图使用此处描述的 onChange 方法保存用户的输入:https: //facebook.github.io/react/docs/forms.html

我的输入有这行代码:

      <input type="text" onChange={this.changeTitle} ref="title" value={this.props.quiz ? this.getTitle() : ''}></input>
Run Code Online (Sandbox Code Playgroud)

但是,当我this.refs.title.value按空格键后拨打电话时,空格未注册。我可以注册这个空间吗?

gor*_*181 2

诸如此类的东西

var ChangeValue = React.createClass({

    getInitialState() {
      return { currentValue: '' };
    },

    onValueChange: function (evnt) {
      this.setState({ currentValue: evnt.target.value });
    },

    render: function() {
        return (
          <div>
             <input type="text" onChange={this.onValueChange} />
             <p>
               Current value is: <b>{this.state.currentValue}</b>
             </p>
          </div>
        );
    }
});

React.render(<ChangeValue />, document.body);
Run Code Online (Sandbox Code Playgroud)

只需更改 onValueChange 方法即可执行您需要的操作。实例: http: //jsbin.com/xayowaloxa/edit?html,js ,output