2013-05-17 46 views
0

我遇到了一些复选框验证问题。 我正在使用插件验证:http://bassistance.de/jquery-plugins/jquery-plugin-validation/JQuery验证 - 复选框验证不发布所有值

我有三个复选框,至少有一个必须打勾。我对这些复选框初始表单代码是:

<input type="checkbox" checked name="check_options" value="News">News 
<input type="checkbox" checked name="check_options" value="Events">Events 
<input type="checkbox" checked name="check_options" value="Promotions">Promotions 

验证规则:

$("#commentForm").validate({ 
      rules: { 
       email_options_name: { 
        required: true 
       }, 
       email_options_company: { 
        required: true 
       }, 
       email_options_country: { 
        required: true 
       }, 
       email_option_email: { 
        required: true, 
        email: true 
       }, 
       check_options: { 
        required: true, 
        minlength: 1 
       } 
      },  
     }); 

这里验证了check_options工作正常。然而,处理表单的php脚本只会打印最后一个复选框的值(所以如果打勾了两个或三个,只会发布最后一个值)。

我修改我的输入名称来解决这个问题:

<input type="checkbox" checked name="check_options[]" value="News">News 
<input type="checkbox" checked name="check_options[]" value="Events">Events 
<input type="checkbox" checked name="check_options[]" value="Promotions">Promotions 

这显示了所有在岗的价值观。但是我无法通过验证来为它工作。如果我把我的规则:

check_options[]: { 
       required: true, 
       minlength: 1 
      } 

我得到一个JavaScript错误:语法错误:缺少:物业编号

后,如果我离开它没有方括号验证不会发生。

感谢您的帮助!

回答

1

使用HTML这样的:

<input type="checkbox" checked name="check_options[]" value="News" required minlength="1">News 
<input type="checkbox" checked name="check_options[]" value="Events">Events 
<input type="checkbox" checked name="check_options[]" value="Promotions">Promotions 

和Javascript成为

check_options: { 
      required: true, 
      minlength: 1 
     } 
+0

伟大的作品,谢谢! – user1669256

+0

你也可以在javascript''check_options []''中引用你的名字。 – Ryley