KnockoutJS-更新文本框值而不是OnBlur选项的ViewModel OnChange [英] KnockoutJS - Updating ViewModel OnChange of textbox value instead of OnBlur Options

查看:63
本文介绍了KnockoutJS-更新文本框值而不是OnBlur选项的ViewModel OnChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对KnockoutJS还是陌生的,我喜欢到目前为止所看到的. 当前,当视图模型的可观察属性绑定到 文本框的文本属性(输入类型=文本),即ViewModel 仅在文本框的blur事件上更新.有没有办法 在文本框的change事件上更新视图模型?我试过了 在连接change事件时创建一个自定义绑定处理程序 处理程序放在初始"文本框中,但不知何故不起作用.是 这正确地实现了这个目标吗?还是有更简单的方法?

I'm pretty new to KnockoutJS and I love what I've seen so far. Currently, when an observable property of the View Model is bound to the text property of the text box (input type=text), the ViewModel gets updated only on the blur event of the textbox. Is there a way to update the View Model on the change event of the textbox? I tried creating a custom binding handler on wiring up the change event handler on the text box in the "init", but it somehow did not work. Is this the correct to achieve this goal? Or is there an easier way?

推荐答案

您还可以使用'value'绑定并添加valueUpdate绑定属性以指定何时更新控件:

You can also use a 'value' binding and add the valueUpdate binding attribute to specify when to update your control:

请参阅此处: http://knockoutjs.com/documentation/value-binding.html

<p>Your value: <input data-bind="value: someValue, valueUpdate: 'afterkeydown'" /></p>
<p>You have typed: <span data-bind="text: someValue"></span></p> <!-- updates in real-time     -->

<script type="text/javascript">
var viewModel = {
    someValue: ko.observable("edit me")
};
</script>

这篇关于KnockoutJS-更新文本框值而不是OnBlur选项的ViewModel OnChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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