我正在使用ReactJs开发我的应用程序。通过处理onKeyPress事件,当按下Enter时,我正在尝试提交一个输入文本。它检测其他输入,但不检测enter。我尝试过不同的方法-- event.key
、event.charCode
、event.keyCode
、event.which
……似乎什么都不起作用。
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);
}
},
我也尝试过onKewDown
处理,它会检测到正确的键,但即使它将event.keyCode == 13
求值为true,它也不会执行if块。
发布于 2017-07-07 06:11:50
将e.stopPropagataion
从handleSubtotalBy
迁移到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 || [];
// ...
}
发布于 2017-12-01 04:13:00
https://stackoverflow.com/questions/44958938
复制相似问题