帮助jquery验证插件和复选框

tru*_*ekr 4 jquery jquery-validate

我有一个复选框组,我需要唯一的名称来分别在数据库中存储值.但是,使用jquery validate插件,我无法验证名称不同的组.

<label>Would you like to compete?</label>
      <input type="checkbox"  name="compete1" value="2" class="competecheckbox" >
      <label for="compete_no">No</label>
      <input type="checkbox" name="compete2" value="1" class="competecheckbox">
      <label for="compete_yes">Yes</label>
      <div id="competeyes">
        <label class="competeyestxt" hidden=true>Which Location?</label>
        <textarea rows="4" cols="40" maxlength="2000" name="competeyestextarea" class="competeyestxt" hidden="true">
        </textarea>
      </div>
Run Code Online (Sandbox Code Playgroud)

我尝试添加

$.validator.addClassRules(
"competecheckbox", {required: true, minlength: 1});
Run Code Online (Sandbox Code Playgroud)

这种方法有效,但它显示2条错误消息.每个匹配类'competcheckbox'一个.

问题是即使用户选择了competition1,验证错误消息仍然是竞争2.

当用户选择至少一个复选框时,如何清除这两条消息?

谢谢

tru*_*ekr 10

我最终得到了这样的东西:

 <label>Would you like to compete?</label>
      <input type="checkbox"  name="compete[]" value="2" class="competecheckbox" >
      <label for="compete_no">No</label>
      <input type="checkbox" name="compete[]" value="1" class="competecheckbox">
      <label for="compete_yes">Yes</label>
      <div id="competeyes">
        <label class="competeyestxt" hidden=true>Which Location?</label>
        <textarea rows="4" cols="40" maxlength="2000" name="competeyestextarea" class="competeyestxt" hidden="true">
        </textarea>
      </div>


When the form is submitted you get values in an array as compete[1], compete[2] etc.
I didn't know that you could do an empty array for form variable names.
Every day I find out how ignorant I am :)
Run Code Online (Sandbox Code Playgroud)

验证规则看起来像

$('#survey').validate( {
    rules: {
        "compete[]": {
            required: true,
            minlength: 1
        }
  });

The quotes around "compete[]" are required as explained here:
Run Code Online (Sandbox Code Playgroud)

http://docs.jquery.com/Plugins/Validation/Reference#Fields_with_complex_names_.28brackets.2C_dots.29