我已经包括:
knockout.3.0.1.js
knockout.validation.debug.js
jquery.validate.js
错误是:(来自Firebug)
TypeError: self.errors is not a function
除了验证之外,所有其他代码都是完美的。
我的代码
var UserViewModel = function (UserName, Password) {
var self = this;
self.UserName = ko.observable(UserName).extend({
required: {
message:"Please enter User Name"
}
});
self.Password = ko.observable(Password).extend({
required: {
message: "Please enter Password"
}
});
this.loginClick = function () {
if (self.errors().length == 0)
{
alert('no errors');
// var strJSON = ko.toJSON(self); //Convert whole model to json
}
else
{
alert("Please check your submission");
self.errors.showAllMessages();
}
alert("function called");
};
};
ko.applyBindings(new UserViewModel('', ''));
答案 0 :(得分:4)
您忘了创建验证组:
this.errors = ko.validation.group(self);
创建验证组,错误将被解决