gpt4 book ai didi

knockout.js - KnockoutJS 设置输入值

转载 作者:行者123 更新时间:2023-12-01 11:26:18 24 4
gpt4 key购买 nike

我试图在运行 ajax 函数后清除 input 中的值。我正在使用一个简单的函数:$('input[name=foo]').val('')。输入清除但按钮(根据输入中的字符数启用或禁用)不会再次禁用:

JS:

function viewModel(){
var self = this;

//input
self.foo = ko.observable("");
self.eightChars = ko.pureComputed(function(){
return self.foo().length > 7;
});

//button
self.enable = ko.observable("");
self.enableOk = ko.pureComputed(function(){
return self.eightChars();
});
};
ko.applyBindings(viewModel);

HTML:

<input type='text' name='foo' placeholder='Foo' autocomplete='off' data-bind='textInput:foo'>
<button type="button" data-bind='enable:self.enableOk()'>Save</button>

最佳答案

change 事件被触发后 observable 将被通知,所以要尝试像这样更改值:

$('input[name=foo]').val('').change();

Or you can set the value of the observable directily and not the input val, but you need the correct scope to do this.

self.foo('');

Note: Some dev's use your approach, that access the DOM element to clear it as a workaround. So the best approach is use the observable property, because that what the MVVM do.

关于knockout.js - KnockoutJS 设置输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37032151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com