admin管理员组文章数量:1279243
I have this very simple example that works as expected: /
var Hello = React.createClass({
getInitialState: function() {
return { msg: '' }
},
onSubmit: function(e) {
e.preventDefault();
this.setState({ msg: 'submitted' })
},
render: function() {
return (
<form onSubmit={this.onSubmit}>
<input type="text" />
<div>{this.state.msg}</div>
</form>
)
}
});
However, when adding another form field, the onSubmit is not triggered anymore when pressing the enter key: /
var Hello = React.createClass({
getInitialState: function() {
return { msg: '' }
},
onSubmit: function(e) {
e.preventDefault();
this.setState({ msg: 'submitted' })
},
render: function() {
return (
<form onSubmit={this.onSubmit}>
<input type="text" />
<input type="text" />
<div>{this.state.msg}</div>
</form>
)
}
});
Am I missing the obvious here?
I have this very simple example that works as expected: https://jsfiddle/x1suxu9h/
var Hello = React.createClass({
getInitialState: function() {
return { msg: '' }
},
onSubmit: function(e) {
e.preventDefault();
this.setState({ msg: 'submitted' })
},
render: function() {
return (
<form onSubmit={this.onSubmit}>
<input type="text" />
<div>{this.state.msg}</div>
</form>
)
}
});
However, when adding another form field, the onSubmit is not triggered anymore when pressing the enter key: https://jsfiddle/nyvt6506/
var Hello = React.createClass({
getInitialState: function() {
return { msg: '' }
},
onSubmit: function(e) {
e.preventDefault();
this.setState({ msg: 'submitted' })
},
render: function() {
return (
<form onSubmit={this.onSubmit}>
<input type="text" />
<input type="text" />
<div>{this.state.msg}</div>
</form>
)
}
});
Am I missing the obvious here?
Share Improve this question asked Nov 3, 2016 at 11:43 David HellsingDavid Hellsing 109k44 gold badges180 silver badges214 bronze badges 3- Possible duplicate of Submitting a form by pressing enter without a submit button – Fabian Schultz Commented Nov 3, 2016 at 11:55
- just add submit button into your form – Beginner Commented Nov 3, 2016 at 11:56
- It's not a React based problem. w3/TR/html5/forms.html#implicit-submission / stackoverflow./questions/477691/… / stackoverflow./questions/925334/… – thirtydot Commented Nov 3, 2016 at 11:56
3 Answers
Reset to default 11Normally just pressing enter while focusing a form element doesn't submit it, you would use something to specifically submit it (like a submit button or an event handler to submit it upon pressing enter)
However, by default, if there's only ONE text input in the whole form then pressing enter submits it (it's some HTML specification). However, it's good to manually handle form submission.
So if you add this:
<button type='submit' />
to your form, whether there's one or two or 50 text inputs it will still submit
Including an input of type "submit" will suffice for the form submission
<input type="submit">
You can use onKeyPress
instead of onSubmit
as a workaround, something like this
var Hello = React.createClass({
getInitialState: function() {
return { msg: '' }
},
onSubmit: function(e) {
e.preventDefault();
console.log(e.currentTarget); // <-- Form
this.setState({ msg: 'submitted' })
},
onKeyPress: function(e) {
if(e.key === 'Enter')
this.onSubmit(e);
},
render: function() {
return (
<form onKeyPress={this.onKeyPress}>
<input type="text" />
<input type="text" />
<div>{this.state.msg}</div>
</form>
)
}
});
jsfiddle
本文标签: javascriptMultiple react form inputs disables the onSubmitStack Overflow
版权声明:本文标题:javascript - Multiple react form inputs disables the onSubmit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741236333a2363089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论