這是我的表單和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 –