HTML
<button data-bind="enable: isFormValid">My Button</button>
<input type="text" data-bind="value: text1, valueUpdate: 'keyup'" />
<input type="text" data-bind="value: text2, valueUpdate: 'keyup'" />
<input type="text" data-bind="value: text3, valueUpdate: 'keyup'" />
<input type="text" data-bind="value: text4, valueUpdate: 'keyup'" />
JS:
var vm = {
text1: ko.observable(""),
text2: ko.observable(""),
text3: ko.observable(""),
text4: ko.observable("")
}
vm.isFormValid = ko.computed(function() {
return this.text1() && this.text2() && this.text3() && this.text4();
}, vm);
ko.applyBindings(vm);
見更新JSFiddle。解決視圖模型間屬性依賴關係的關鍵是Knockout's computed observables。
驗證:https://github.com/Knockout-Contrib/Knockout-Validation – Anders
@Anders感謝您的有用的鏈接,但我已經使用parsley.js的表單驗證 – SpringLearner
@KyleMit感謝您使我的職務更好看 – SpringLearner