我正在构建一个表单页面,当我想实现“输入键”功能以同时触发验证和方法时,我陷入了困境。
Here's a JS-Fiddle of the example
如您所知,您需要按两次Enter键才能触发该方法。我相信基因敲除验证具有它自己的事件绑定,也许这就是为什么
<input type="text" data-bind="event: {'keypress': enterKey}, value: customer.telephone">
<input type="button" data-bind="click: sendCustomer" value="send">
enterKey = function (d, e) {
if (e.keyCode == 13) {
alert("enter has been pressed..");
sendCustomer();
}
return true;
}
最佳答案
按键和按键之间有区别。此link可能有用。
所以我用keyup代替了onkeypress
<input type="text" data-bind="event: {'keyup': enterKey}, value: customer.telephone">
似乎很有效。希望这对您有所帮助。
JSFIDDLE