将Javascript onkeypress转换为基因敲除js以在enter上调用 [英] Convert Javascript onkeypress to knockoutjs to call on enter
本文介绍了将Javascript onkeypress转换为基因敲除js以在enter上调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尽力在KnockoutJS中做所有事情,但是我很难把它转换成Knockoutjs.
I am trying to do everthing I can in KnockoutJS however I am having a hard time getting this to convert to knockoutjs.
我有一个输入框,在按Enter时,我需要调用addInputName().我认为这是一种过时的方式.有没有办法在淘汰赛中做到这一点?
I have an input box that upon enter press I need to call addInputName(). This is kind of the old school way I think to do it. Is there a way to do this all in knockout?
<input id="inputName" onkeypress="addInputName(this, event);" />
<input id="addInputName" type="button" data-bind="event: { click: addInputName }" value="Add" />
self.addInputName = function (inputElement, event) {
if (event.keyCode == 13) {
$('#addInputName').click();
}
};
推荐答案
// View
<input id="inputName" data-bind="value: name, enterKey: addInputName" />
<input id="addInputName" type="button" data-bind="click: addInputName" value="Add" />
// ViewModel
function ViewModel() {
var self = this;
self.name = ko.observable();
self.names = ko.observableArray();
self.addInputName = function () {
self.names.push(self.name());
self.name("");
};
}
// Custom Binding
ko.bindingHandlers.enterKey = {
init: function (element, valueAccessor, allBindings, data, context) {
var wrapper = function (data, event) {
if (event.keyCode === 13) {
valueAccessor().call(this, data, event);
}
};
ko.applyBindingsToNode(element, { event: { keyup: wrapper } }, context);
}
};
自定义绑定 @ 20:05
查看自定义绑定.这是帮助您从ViewModel的业务逻辑中摆脱UI逻辑的宝贵工具.
Look into Custom Bindings. It's an invaluable tool to help get UI logic out of your ViewModel's business logic.
这篇关于将Javascript onkeypress转换为基因敲除js以在enter上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文