按 Enter 键后调用 onChange 事件 [英] to call onChange event after pressing Enter key
本文介绍了按 Enter 键后调用 onChange 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Bootstrap 的新手并且一直遇到这个问题.我有一个输入字段,只要我输入一个数字,就会调用 onChange
中的函数,但我希望在输入整个数字后按Enter"键时调用它.验证函数也有同样的问题——它调用得太快了.
I am new to Bootstrap and stuck with this problem. I have an input field and as soon as I enter just one digit, the function from onChange
is called, but I want it to be called when I push 'Enter when the whole number has been entered. The same problem for the validation function - it calls too soon.
var inputProcent = React.CreateElement(bootstrap.Input, {type: "text",
//bsStyle: this.validationInputFactor(),
placeholder: this.initialFactor,
className: "input-block-level",
onChange: this.handleInput,
block: true,
addonBefore: '%',
ref:'input',
hasFeedback: true
});
推荐答案
根据 React Doc,您可以监听键盘事件,例如 onKeyPress
或 onKeyUp
,而不是 onChange
.
According to React Doc, you could listen to keyboard events, like onKeyPress
or onKeyUp
, not onChange
.
var Input = React.createClass({
render: function () {
return <input type="text" onKeyDown={this._handleKeyDown} />;
},
_handleKeyDown: function(e) {
if (e.key === 'Enter') {
console.log('do validate');
}
}
});
更新:使用 React.Component
这是使用 React.Component 做同样事情的代码
Update: Use React.Component
Here is the code using React.Component which does the same thing
class Input extends React.Component {
_handleKeyDown = (e) => {
if (e.key === 'Enter') {
console.log('do validate');
}
}
render() {
return <input type="text" onKeyDown={this._handleKeyDown} />
}
}
这是jsfiddle.
const Input = () => {
const handleKeyDown = (event) => {
if (event.key === 'Enter') {
console.log('do validate')
}
}
return <input type="text" onKeyDown={handleKeyDown} />
}
这篇关于按 Enter 键后调用 onChange 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文