JavaScript更改文本框后,Knockout.js强制更改 [英] Knockout.js force change after JavaScript changes a textbox

查看:51
本文介绍了JavaScript更改文本框后,Knockout.js强制更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入内容:

<input id="MyTextBox" type="text" data-bind="value: MyTextBox" />

如果我自己更改文本框,则数据绑定有效...但是,如果我使用JQuery设置文本框的文本,则文本框会更改,但不会发生数据绑定.

If I change the textbox myself then data binding works... but if I set the text of the textbox with JQuery the textbox changes but no data bind occurs.

$("#MyTextBox").val("Some text");

如何在没有任何击键的情况下更改文本框,并且仍然可以正常工作?

How can I change a textbox without any keystrokes and still get binding to work?

谢谢

推荐答案

使用Knockout时,通常不应该使用其他方法来修改视图.因此,不要使用jQuery来更改文本框的值,而是通过修改绑定到的可观察对象来使用Knockout:

When using Knockout, you generally shouldn't be using something else to modify the view. So instead of using jQuery to change the textbox value, use Knockout by modifying the observable it's bound to:

this.MyTextBox("Some text");

如果您确实需要在Knockout之外修改文本框,可以通过生成更改事件来表示value绑定:

If you really need to modify the textbox outside of Knockout, you can signal the value binding by generating a change event:

$("#MyTextBox").val("Some text").change();

这篇关于JavaScript更改文本框后,Knockout.js强制更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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