简体   繁体   English

在knockoutjs上绑定keypress事件,可观察到没有填充

[英]Binding keypress event on knockoutjs, observable not populated

Need a little help with knockoutjs and binding a keypress event. 需要一些关于knockoutjs和绑定按键事件的帮助。 I'm trying to hook up knockout so that I pick up on the enter keypress from within a text box. 我正试图连接淘汰赛,以便我从文本框中选择输入按键。 So I can perform the same action as clicking a button. 所以我可以执行与单击按钮相同的操作。 Its a little tricky to explain but hopefully this JsFiddle will demonstrate what I'm trying to achieve. 解释起来有点棘手但希望这个JsFiddle会展示我想要实现的目标。

http://jsfiddle.net/nbnML/8/ http://jsfiddle.net/nbnML/8/

The problem I have is that observable value is not getting updated and I think its something to do with an observable not being updated until focus moves away from the textbox? 我遇到的问题是可观察的值没有得到更新,我认为它与一个observable有关,直到焦点离开文本框才更新?

Any solutions to this problem. 解决这个问题的任何方法。

Thanks! 谢谢!

One option is to use the valueUpdate additional binding to force an update on each keypress. 一种选择是使用valueUpdate附加绑定来强制每次按键更新。 For example, you would do: 例如,你会这样做:

<input type="text" data-bind="value: InputValue, valueUpdate: 'afterkeydown', event: { keypress: RunSomethingKey }" />

If that is not what you are after, then really you would want to fire the element's change event in your handler. 如果那不是您所追求的,那么您真的想要在处理程序中触发元素的更改事件。 For example with jQuery, you would do something like: $(event.target).change(); 例如,使用jQuery,您可以执行以下操作: $(event.target).change(); .

It would be better though to move this into a custom binding. 将它移动到自定义绑定会更好。 Maybe something like (probably should check if the result of valueAccessor() is a function): 也许是这样的(可能应该检查valueAccessor()的结果是否是一个函数):

ko.bindingHandlers.enterKey = {
    init: function(element, valueAccessor, allBindings, vm) {
        ko.utils.registerEventHandler(element, "keyup", function(event) {
            if (event.keyCode === 13) {
                ko.utils.triggerEvent(element, "change");
                valueAccessor().call(vm, vm); //set "this" to the data and also pass it as first arg, in case function has "this" bound
            }

            return true;
        });
    }         
};

Here is your sample updated: http://jsfiddle.net/rniemeyer/nbnML/9/ 以下是您更新的示例: http//jsfiddle.net/rniemeyer/nbnML/9/

Don't discount submit bindings: http://knockoutjs.com/documentation/submit-binding.html 不要打折提交绑定: http//knockoutjs.com/documentation/submit-binding.html

This takes care of some IE 9/10 gotchas such as the return key not updating the observable. 这会处理一些IE 9/10陷阱,例如返回键没有更新observable。 With this taken care of you don't need to intercept keycode 13 有了这个照顾你,你不需要拦截键码13

html: HTML:

<form data-bind="submit:RunSomething">
 <input type="text" data-bind="value: InputValue" />
 <input type="submit" value="test" />
 <div data-bind="text: InputValue" />
</form>

code: 码:

var ViewModel = function () {
    var self = this;
    self.InputValue = ko.observable('');

    self.RunSomething = function (ev) {
        window.alert(self.InputValue());
    }
}
ko.applyBindings(new ViewModel());

See this here: 在这看到:

http://jsfiddle.net/jnewcomb/uw2WX/ http://jsfiddle.net/jnewcomb/uw2WX/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM