请帮助解决问题。我通过验证制作表格。点击提交按钮输出短语'Great Success!'后在警报窗口
但我需要在控制台中点击提交按钮获取特定字段(用户名和电子邮件)的结果。我的尝试:
events: {
'click #signUpButton': function (e) {
e.preventDefault();
this.signUp();
}
},
signUp: function () {
var data = this.$el.serializeObject();
this.model.set(data);
var modelValid = this.model.isValid(true);
console.log('model valid', modelValid);
var modelValidUsername = this.model.username.isValid(true);
console.log('model username valid', modelValidUsername);
var modelValidEmail = this.model.email.isValid(true);
console.log('model email valid', modelValidEmail);
if(this.model.isValid(true)){
// this.model.save();
alert('Great Success!');
}
},
所有代码都在这里:http://jsfiddle.net/udXL5/595/
答案 0 :(得分:1)
根据文档(http://thedersen.com/projects/backbone-validation/#isvalid),isValid
也接受属性名称(或属性名称数组)以验证各个字段:
var modelValidUsername = this.model.isValid('username');
console.log('model username valid', modelValidUsername);
var modelValidEmail = this.model.isValid('email');
console.log('model email valid', modelValidEmail);