如何让knockout.js在文本输入值更改后立即更新 View 模型?问题是更新仅在字段失去焦点时才会触发。
"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/