我有一个使用Knockout.js作为ts值的登录表单。
精简示例代码:
<input id="username" placeholder="Email" data-bind="textInput: Profile.Email" required />
<input id="password" type="password" placeholder="Password" data-bind="textInput: Profile.Password" required />
var Profile = function () {
self.Email = ko.observable('');
self.Password = ko.observable('');
}
当像密码管理器Norton Identity Safe这样的程序自动填充您的登录信息(我无法验证其他自动填充来源)时,Profile.Email和Profile.Password的可观察项不会更新。如果我单击该框并赋予该字段焦点,然后离开该字段,则该值仍不会更新。仅当我添加或删除字符时,字段才会更新。
我在这里看到了其他一些有关如何“破解”此问题的线程,但是它们都是KO 3.2.0之前的版本,其中包括textInput
似乎可以解决此问题的绑定,但事实并非如此。
是否有任何原因导致textInput不更新自动填充上的可观察值,并且有任何新方法来获取更新的值却不是那么“ hacky”。
除非change()
触发触发器,否则淘汰赛将不会看到更新的输入值。这是您可以玩的一个小演示。如果选中此复选框,change
则在更新值后触发触发器,然后更改可观察值。
vm = {
uname: ko.observable('initial'),
changeIt: function() {
var $el = $('#username').val('changed!');
if (vm.doTrigger()) {
$el.change();
}
},
doTrigger: ko.observable(false)
};
ko.applyBindings(vm);
vm.uname.subscribe(function(newValue) {
console.debug("Changed:", newValue);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<input id="username" data-bind="textInput: uname" />
<div data-bind="text:uname"></div>
<button data-bind="click:changeIt">Change It</button>
Trigger it: <input type="checkbox" data-bind="checked: doTrigger" />
另请参阅:用jquery更新字段不会更新可观察到的
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句