输入按键的Knockout事件绑定会导致奇怪的行为 [英] Knockout event binding for input keypress causes weird behavior
本文介绍了输入按键的Knockout事件绑定会导致奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
长话短说,我想让用户在输入元素上输入enter,并调用我的viewmodel中的某个方法。这是我的html输入:
Long story short, I want to enable users to hit enter on an input element and certain method in my viewmodel be called. Here is my html input:
<input id="searchBox" class="input-xxlarge" type="text" data-bind="value: searchText, valueUpdate: 'afterkeydown', event: { keypress: $parent.searchKeyboardCmd}">
这是我在vm中的方法:
and here is my method in vm:
searchKeyboardCmd = function (data, event) { if (event.keyCode == 13) searchCmd(); };
一切正常, searchCmd
在调用时调用我输入输入,但问题是我可以在输入中输入任何内容,即我输入输入的所有内容都将被忽略。提前感谢您的帮助。
everything works fine and searchCmd
is called when I hit enter on input, but the problem is that I can type nothing in input, i.e. everything I type into input is ignored. Thank you in advance for your help.
推荐答案
根据KO文档,您必须返回 true 如果您希望默认操作继续,请从您的事件处理程序中获取code>。
According to KO docs you have to return true
from your event handler if you want the default action proceed.
searchKeyboardCmd = function (data, event) {
if (event.keyCode == 13) searchCmd();
return true;
};
这篇关于输入按键的Knockout事件绑定会导致奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文