admin管理员组文章数量:1287581
I am using ReactJs to develop my application. I am trying to submit an input text when Enter is pressed by handling the onKeyPress event. It detects other inputs, but not enter. I have tried different ways - event.key
, event.charCode
, event.keyCode
, event.which
... Nothing seems to work.
React.createElement("input", {tabIndex: "1", onKeyPress: this.handleKeyPress, onChange: this.handleChange,
placeholder: "ex: 1,10,15"}),
handleChange: function (event) {
this.setState({userInputBuckets: event.target.value});
},
handleKeyPress: function (event) {
if (event.charCode == 13) {
event.preventDefault();
this.props.table.handleSubtotalBy(this.props.columnDef, this.state.userInputBuckets);
}
},
I also tried with onKewDown
handling, it detects the correct key, but it doesn't execute the if block even though it evaluates event.keyCode == 13
as true.
I am using ReactJs to develop my application. I am trying to submit an input text when Enter is pressed by handling the onKeyPress event. It detects other inputs, but not enter. I have tried different ways - event.key
, event.charCode
, event.keyCode
, event.which
... Nothing seems to work.
React.createElement("input", {tabIndex: "1", onKeyPress: this.handleKeyPress, onChange: this.handleChange,
placeholder: "ex: 1,10,15"}),
handleChange: function (event) {
this.setState({userInputBuckets: event.target.value});
},
handleKeyPress: function (event) {
if (event.charCode == 13) {
event.preventDefault();
this.props.table.handleSubtotalBy(this.props.columnDef, this.state.userInputBuckets);
}
},
I also tried with onKewDown
handling, it detects the correct key, but it doesn't execute the if block even though it evaluates event.keyCode == 13
as true.
- Is this input being created in a form? It might be firing the submit event instead of keypress. Have you tried the keyDown event and seeing if it can capture the key stroke? – Deadron Commented Jul 6, 2017 at 21:18
- Yes, I tried with onKeyDown as well. It's bypassing the if block after evaluating the condition to true. – saumj Commented Jul 6, 2017 at 21:21
- "it doesn't execute the if block even though it evaluates event.keyCode == 13 as true" that isn't right, unless you're using a very buggy JS engine. It's likely that the code inside throws an Error. – joews Commented Jul 6, 2017 at 21:29
-
I tried that again. It executes the block, but results in an error at
event.stopPropogation()
at some other place where event is undefined. – saumj Commented Jul 6, 2017 at 21:39 - OK. You'll need to post that part of the code to get some help. – joews Commented Jul 6, 2017 at 21:55
2 Answers
Reset to default 5Move e.stopPropagataion
from handleSubtotalBy
into handleKeyPress
:
handleKeyPress(event) {
if (event.charCode == 13) {
event.preventDefault();
event.stopPropagation();
this.props.table.handleSubtotalBy(this.props.columnDef, this.state.userInputBuckets);
}
}
handleSubtotalBy(columnDef, partitions) {
const subtotalBy = this.state.subtotalBy || [];
// ...
}
Given answer is not plete,
use
var code = event.keyCode || event.charCode
to support all browser:
Other than this, given answer is correct. Copy that.
本文标签: javascriptHow to detect 39Enter39 key on keyPress when used with ReactStack Overflow
版权声明:本文标题:javascript - How to detect 'Enter' key on keyPress when used with React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741312469a2371724.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论