Pej*_*man 6 keypress html-input javascript-events mvvm knockout.js
长话短说,我想让用户在输入元素上输入enter并调用我的viewmodel中的某个方法.这是我的html输入:
<input id="searchBox" class="input-xxlarge" type="text" data-bind="value: searchText, valueUpdate: 'afterkeydown', event: { keypress: $parent.searchKeyboardCmd}">
Run Code Online (Sandbox Code Playgroud)
这是我在vm中的方法:
searchKeyboardCmd = function (data, event) { if (event.keyCode == 13) searchCmd(); };
Run Code Online (Sandbox Code Playgroud)
一切正常,searchCmd当我输入输入时调用,但问题是我可以在输入中输入任何内容,即我输入输入的所有内容都将被忽略.预先感谢您的帮助.
And*_*ins 20
根据KO文档,true如果您希望默认操作继续,则必须从事件处理程序返回.
searchKeyboardCmd = function (data, event) {
if (event.keyCode == 13) searchCmd();
return true;
};
Run Code Online (Sandbox Code Playgroud)