我有这个 jsFiddle http://jsfiddle.net/63qJe/3/可以工作,但没有显示验证方法。
HTML:
<input type="checkbox" data-bind="checked: approveTerms"/>
<label for="algvw">Yes, I agree to the general terms and conditions</label>
<span data-bind="validationMessage: approveTerms"></span>
<button type="button" data-bind="click: stepForward">Commit order</button>
JS:
ko.validation.rules['checkedTerms'] = {
validator: function (value) {
console.log(value);
if (!value)
return false;
return true;
}
};
function ovm() {
var self = this;
self.approveTerms = ko.observable(false).extend({
checkedTerms: { message: 'Approval required' }
});
self.currentStep = ko.observable(1);
self.stepForward = function () {
if (self.currentStep() === 1)
commitOrder(self);
};
}
window.vm = new ovm();
ko.applyBindings(vm);
function commitOrder(vm) {
if (vm.approveTerms())
alert("GO!");
else
alert("Nooooooooo");
}
我对 KO 还很陌生,仍在学习。我在文本字段上有其他验证器并且这些验证器可以工作,但是这个复选框让我很头疼。
为什么我不能这样做:
self.approveTerms = ko.observable(false).extend({ required: true, message:'approval req.' });