当我将Observable
变量绑定到input
值和keydown
事件时,它不会更新可观察值
function vModel() {
var vm = this;
vm.value = ko.observable();
vm.keyDown = function() {
};
}
ko.applyBindings(new vModel());

<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<input type="text" data-bind="value: value, event: {keydown: keyDown}">
<span data-bind="text: value"></span>
&#13;
如何在同一输入元素中绑定value
和event: {keydown: myKeyDown}
?
答案 0 :(得分:2)
您可以在事件监听器中返回true
,以确保按键关闭使其进入输入。
但是,您可能希望查看textInput
绑定(textInput: value
)并订阅value
而不是......
function vModel() {
var vm = this;
vm.value = ko.observable();
vm.keyDown = function() {
console.log("key down");
return true;
};
}
ko.applyBindings(new vModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<input type="text" data-bind="value: value, event: {keydown: keyDown}">
<span data-bind="text: value"></span>