是否无法在viewmodel中使用isvalid方法?我的错误是有效的功能无效。
function Student(name,age,country) {
var self = this;
self.Name = ko.observable(name).extend({ required: true });
self.Age = ko.observable(age);
self.Country = ko.observable(country);
};
/*----------------------------------------------------------------------*/
/* View Model
/*----------------------------------------------------------------------*/
function StudentViewModel() {
var self = this;
self.students = ko.observableArray([]);
self.countries =ko.observableArray([]);
this.errors = ko.validation.group(this.students, { deep: true });
self.editingItem = ko.observable();
self.isItemEditing = function(itemToTest) {
return itemToTest == self.editingItem();
};
self.addStudent = function () {
var student = new Student("New name","0", self.countries[0]);
self.students.push(student);
// begin editing the new item straight away
self.editStudent(student);
};
self.removeStudent = function (student) {
if (student.Name()) {
if (confirm('Are you sure you wish to delete this item?')) {
if (self.editingItem() == null) {
self.students.remove(student);
}
}
}
};
self.editStudent = function (student) {
// if (self.editingItem() == null) {
// shows the edit fields
self.editingItem(student);
// }
};
self.applyStudent = function (student) {
// hides the edit fields
//jQuery("#form1").valid();
//alert(self.isValid());
alert(self.isValid());
if(self.isValid())
{
self.editingItem(null);
}
};
self.cancelStudent = function (student) {
// hides the edit fields
self.editingItem(null);
};
答案 0 :(得分:1)
您使用的是Knockout Validation插件吗?如果是,则StudentViewModel
包含在ko.validatedObservable()
中,isValid()
方法位于外部验证的可观察对象上。 self
指向内部StudentViewModel
,因此无法定义isValid()
。