jquery – 验证插件 – 自动验证页面上的多个表单

前端之家收集整理的这篇文章主要介绍了jquery – 验证插件 – 自动验证页面上的多个表单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
目前我需要验证每一个这样的形式:
  1. $(document).ready(function () {
  2. $('#admin_settings_general').validate({
  3. rules: {
  4. admin_settings_application_title: {
  5. required: true
  6. }
  7. },highlight: function (element) {
  8. $(element).closest('.form-group').addClass('has-error');
  9. },unhighlight: function (element) {
  10. $(element).closest('.form-group').removeClass('has-error');
  11. }
  12. });
  13. });

我想要它自动验证每个元素的窗体与所需的标签

我怎样才能做到这一点?

解决方法

报价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()。这就是这个插件方法的设计方法

  1. $(document).ready(function() {
  2.  
  3. $('form').each(function() { // attach to all form elements on page
  4. $(this).validate({ // initialize plugin on each form
  5. // global options for plugin
  6. });
  7. });
  8.  
  9. });

工作演示:http://jsfiddle.net/6Fs9y/

猜你在找的jQuery相关文章