这是我的表单和onClick方法。我想在按下键盘上的Enter按钮时执行此方法。怎么样 ?如何使用react.js中的Enter键提交表单?
N.B:没有jquery的赞赏。
comment: function (e) {
e.preventDefault();
this.props.comment({comment: this.refs.text.getDOMNode().value, userPostId:this.refs.userPostId.getDOMNode().value})
},
<form className="commentForm">
<textarea rows="2" cols="110" placeholder="****Comment Here****" ref="text" /><br />
<input type="text" placeholder="userPostId" ref="userPostId" /> <br />
<button type="button" className="btn btn-success" onClick={this.comment}>Comment</button>
</form>
为什么的onsubmit = {} this.onCommentSubmit> @Dominic –
@JasonBourne你可以给回调任何你想要的名字,我只是一直给事件回调的名称,如? onSomethingClick,onMouseMove,onFormKeyPress等,而不是根据它应该在内部做什么来命名方法,因为有时会改变或者在另一种方法中(更多可测试) –
我遵循了你的指示,但是,在鼠标单击时它是不工作。虽然它在为Enter键工作,但当我用Tabs键将我的选择悬停在按钮上时它正在工作。 你能提出一个更好的方法吗? @Dominic –