使用jquery更新字段不会更新observable [英] Update field with jquery don't update observable

查看:87
本文介绍了使用jquery更新字段不会更新observable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向stackoverflow社区问好。



我几天前刚开始使用淘汰赛。



正确知道我正在使用它为我正在使用的CMS制作动态菜单构建器。



以下是代码: http://jsfiddle.net/dnlgmzddr/HcRqn/



<问题是,当我从选择框中选择一个元素时,输入字段会按照我的预期更新,但是observable不会反映更改。因此,添加按钮未启用。



我缺少什么?我该如何解决?



谢谢。

解决方案

如果填充了url字段,则需要触发change事件以使observable得到提升。所以,你可以这样做:

  $(#url)。val('/ pages /'+ id).change (); 

除了Knockout精神更多的另一个选择是在你的选择上使用绑定。在这种情况下,您可能希望使用该值填充observable,然后使用手动订阅将格式化的值默认为输入字段。

  this.itemUrl = ko.observable(); 
this.selectedUrl = ko.observable();
this.selectedUrl.subscribe(function(newValue){
if(newValue){
this.itemUrl(/ pages /+ newValue);
}
}, 这个);

然后,将您的选择绑定到 selectedUrl

 < select id =pagedListdata-bind =value:selectedUrl> 
< option value =>< option>
< option value =test>测试< /选项>
< / select>

以下是一个示例: http://jsfiddle.net/rniemeyer/HcRqn/21/



您还可以消除额外的可观察性如果您的选项的价值是网址,则需要手动订阅。


I want to say hello to the stackoverflow community.

I've just started using knockout a few days ago.

Right know I'm using it to make a dynamic menu builder for a CMS I'm working on.

Here is the code: http://jsfiddle.net/dnlgmzddr/HcRqn/

The problem is that when I choose an element from the select box, the input field update as I expect, but the observable doesn't reflect the change. Because of that, the add button is not enabled.

What am I missing? How can I fix it?

Thank you.

解决方案

When you populate the url field, you would need to trigger the change event to get the observable to be upated. So, you could do:

$("#url").val('/pages/' + id).change(); 

Another option though that is more in the Knockout spirit is to use a binding on your select. In this case, you would likely want to populate an observable with that value, then use a manual subscription to default the formatted value into the input field.

this.itemUrl = ko.observable();
this.selectedUrl = ko.observable();
this.selectedUrl.subscribe(function(newValue) {
    if (newValue) {
        this.itemUrl("/pages/" + newValue);                
    }
}, this);

Then, bind your select to selectedUrl:

    <select id="pagedList" data-bind="value: selectedUrl">
        <option value=""><option>
        <option value="test">Test</option>
    </select>

Here is a sample: http://jsfiddle.net/rniemeyer/HcRqn/21/

You could also eliminate the extra observable and manual subscription if the "value" of your options was the url.

这篇关于使用jquery更新字段不会更新observable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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