javascript - 文本框值更改后立即更新 Knockout.js View 模型

标签 javascript html data-binding mvvm knockout.js

如何让knockout.js在文本输入值更改后立即更新 View 模型?问题是更新仅在字段失去焦点时才会触发。

来自knockout.js documentation :

"afterkeydown" is the best choice if you want to keep your view model updated in real-time.

我发现这没有帮助。在我的系统中,我有一个名为用户 token 的字段。因此用户经常使用鼠标粘贴 token 。

那么如何让 knockout.js 在文本字段中的每次文本更改时立即更新其模型。

最佳答案

正如您在问题中提到的,这是由 valueUpdate binding 控制的.

如果您只需要支持“现代浏览器”,则建议使用

input 值。 “非现代浏览器”基本上是指 IE8 及更早版本。来自文档:

Of [all the valueUpdate] options, input is the best choice if you want to keep your view model updated in real-time, and you only need to support reasonably modern browsers such as IE 9+ (whereas "afterkeydown" is a good choice for older browsers). For example:

<p>Your value: <input data-bind="value: someValue, valueUpdate: 'input'" /></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>

关于javascript - 文本框值更改后立即更新 Knockout.js View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12868290/

相关文章:

c# - 从浏览器访问本地文件?

javascript - 如何将列表项更改为 react 中编辑功能的输入?

javascript - 使用 moovweb 转换类别下拉列表以切换

wpf - 从 ViewModel 绑定(bind) WPF ComboBox 的数据 - 无法更改所选项目

post - chalice 3 : Binding multiple command objects via POST

c# - 通过 DataTemplate 为 WPF-ComboBoxItem 设置 TextSearch.Text

javascript - 如何在 React 中正确地进行 GET 调用返回一个可观察对象(类似于 Angular 中的方法而不是使用 promise )?

javascript - 为什么这个 jQuery 插件(Tooltipster)的 Ajax 内容只有在鼠标悬停 2 次后才显示?

javascript - "Chart isn' t 声明错误“我的代码在 Codepen 中完美运行,但是在本地运行时我不断收到错误

html - 调整 div 容器大小,保持每个元素水平对齐