目前我需要验证每一个这样的形式:
$(document).ready(function () {
$('#admin_settings_general').validate({
rules: {
admin_settings_application_title: {
required: true
}
},highlight: function (element) {
$(element).closest('.form-group').addClass('has-error');
},unhighlight: function (element) {
$(element).closest('.form-group').removeClass('has-error');
}
});
});
我想要它自动验证每个元素的窗体与所需的标签。
我怎样才能做到这一点?
解决方法
报价OP:
“I want that it automaticly validate the forms for every element with the required tag.”
报价OP评论:
“i have to call
$('#admin_settings_general').validate()for each of the forms currently. How can i call it without limiting it to one form?”
要在页面上的所有表单上正确初始化.validate(),请使用公共选择器,例如表单标签本身(或类)。由于您无法将.validate()附加到表示多个表单元素的任何jQuery选择器,请使用jQuery .each()。这就是这个插件方法的设计方法。
$(document).ready(function() {
$('form').each(function() { // attach to all form elements on page
$(this).validate({ // initialize plugin on each form
// global options for plugin
});
});
});
工作演示:http://jsfiddle.net/6Fs9y/