输入值更改时的Knockout值绑定除了'afterkeydown'之外不起作用 [英] Knockout value binding when an input value changes do not work other than 'afterkeydown'

查看:105
本文介绍了输入值更改时的Knockout值绑定除了'afterkeydown'之外不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有敲除装订的文本框如下。

I have a text box with knockout binding as follows.

<input style="width: 385px; float: right" type="text" id="SymptomBox" placeholder="Symptoms" data-bind ="value : acceptedSymptom,valueUpdate:['afterkeydown'],event:{keypress:AddSymptoms}"/>





我的视图模型如下



My view model is as follows

var ViewModel = function (data) {
var self=this;
self.acceptedSymptom=ko.observable();
}
ko.applyBindings(ViewModel);



每次按下键盘键,我都可以更新可观察数组acceptedSymptom。显然,这是由于valueUpdate:['afterkeydown']属性绑定到我的输入文本框。这很好,但我也希望可观察数组更新值,而我更改输入文本框除了keydown之外的值。例如,我有一个JQuery自动完成插件集成到上面提到的文本框。虽然我通过鼠标点击下拉选择任何菜单项,observablearray应该改变值与输入文本框的值的变化。请注意,如果我选择下拉值,使用键盘它工作正常。只有问题是用鼠标指针选择不更新可观察数组。除了'afterkeydown',我还尝试了valueUpdate:['input','afterkeydown','change','blur','负载']。这些都没有任何意义。请帮助我....


I can update the observable array "acceptedSymptom" each time i press a key with key board.Clearly it is due to valueUpdate:['afterkeydown'] attribute binding to my input text box.This works fine,but i also want observable array to update value while i change value of input text box other than keydown.For example,i have a JQuery autocomplete plugin integrated to above mentioned text box.While i select any menu item from drop down by mouse click,observablearray should have changed value with change in value of input text box.It is noted that if i am selecting drop down values,using keyboard it works fine.Only issue is with selection with mouse pointer do not update observable array.Other than 'afterkeydown', i have also tried valueUpdate:['input','afterkeydown','change','blur','onLoad']. None of these make any sense.Please help me....

推荐答案

亲爱的Jitendra,



实际上,如果您使用的是knockoutJS,这意味着您的设计以模型为中心。因此,如果你的自动完成返回一些值 - 最逻辑上是改变模型的值,而不是它在输入中的表示。



如果你仍然坚持改变价值输入,只是在操作后强制触发更改事件:



Dear Jitendra,

Actually if you are using knockoutJS, this means that your design is centered around the model. Thus, if your autocomplete returns some value - most logically is to change value of the model, rather than it's presentation in input.

If you still insist on changing value of the input, just force triggering "change" event after the operation:

document.getElementById("changeit").addEventListener("click", function(){
    document.getElementById("SymptomBox").value = "new value";


#SymptomBox)。trigger( 变化);
});
("#SymptomBox").trigger("change"); });





简单示例:





http://jsfiddle.net/voronenko/QATTg/ [ ^ ]


这篇关于输入值更改时的Knockout值绑定除了'afterkeydown'之外不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆