使用Jquery验证验证组内的组

时间:2011-12-31 05:55:53

标签: jquery validation

我知道这段代码很糟糕,但除了一部分外,它的工作原理:

如果您选中3个复选框(newsletter,newsletter2或newsletter3)中的任意一个,如果您选择“是”,则我已经在每个组中显示了2个复选框,如图1所示:

Figure 1

但是当“更多?”中选择“是”时下拉菜单首先,我在它下面的3个选项(newsletter,newsletter2& newsletter3)成为必需(你必须选择至少一个选项/ minlength:1),如图2所示:

Figure 2

一切正常并提交,但我仍然坚持这一部分。简而言之,我希望您首先看到的3个选项(在点击它们之前)要求您在验证之前至少点击其中一个选项。

这是迄今为止有效的代码:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script src="js/jquery.validate.js" type="text/javascript"></script>

<script type="text/javascript">
$.validator.setDefaults({
});

$().ready(function() {

    $("#commentForm").validate({

        rules: {

            'entry.7.group': {
                required: "#newsletter:checked",
                minlength: 1
                                            },
            'entry.77.group': {
                required: "#newsletter2:checked",
                minlength: 1
                            },
            'entry.777.group': {
                required: "#newsletter3:checked",
                minlength: 1
                            }
        }
    }); 
    });
</script>

<script language="JavaScript">
  function showhidefield1()
  {
    if (document.mainform.more.value == 'yes')
    {
      document.getElementById("hideablearea1").style.display = "block";
      document.getElementById("newsletter.group").find("input").val("").addClass("required");

    }
    else
    {
      document.getElementById("hideablearea1").style.display = "none";
      document.getElementById("newsletter.group").find("input").val("").removeClass("required");

    }
  }
</script>


<script language="JavaScript">
  function showhidefield2()
  {
    if (document.mainform.newsletter.checked)
    {
      document.getElementById("hideablearea2").style.display = "block";
      document.getElementById("group_7_1").find("input").val("").addClass("required");
      document.getElementById("group_7_2").find("input").val("").addClass("required");
    }
    else
    {
      document.getElementById("hideablearea2").style.display = "none";
      document.getElementById("group_7_1").find("input").val("").removeClass("required");
      document.getElementById("group_7_2").find("input").val("").removeClass("required");
    }
  }
</script>

<script language="JavaScript">
  function showhidefield3()
  {
    if (document.mainform.newsletter2.checked)
    {
      document.getElementById("hideablearea3").style.display = "block";
      document.getElementById("group_77_1").find("input").val("").addClass("required");
      document.getElementById("group_77_2").find("input").val("").addClass("required");
    }
    else
    {
      document.getElementById("hideablearea3").style.display = "none";
      document.getElementById("group_77_1").find("input").val("").removeClass("required");
      document.getElementById("group_77_2").find("input").val("").removeClass("required");
    }
  }
</script>

<script language="JavaScript">
  function showhidefield4()
  {
    if (document.mainform.newsletter3.checked)
    {
      document.getElementById("hideablearea4").style.display = "block";
      document.getElementById("group_777_1").find("input").val("").addClass("required");
      document.getElementById("group_777_2").find("input").val("").addClass("required");
    }
    else
    {
      document.getElementById("hideablearea4").style.display = "none";
      document.getElementById("group_777_1").find("input").val("").removeClass("required");
      document.getElementById("group_777_2").find("input").val("").removeClass("required");
    }
  }
</script>


<style type="text/css">
label.error {
color: #ff0600; list-style-position: outside;
clear: left;
display: inline;
float:right;
height:10px;}
}
</style>

</head>

然后身体里有这个:

      <select name="newsletter4" class="required" id="more" onchange="showhidefield1()">
      <option value="">More?</option><option value="yes">Yes</option><option value="no">No</option></select>
  <div id='hideablearea1' style='display:none'>
<br><br>

            <input type="checkbox" class="checkbox" id="newsletter" name="newsletter.group" onclick="showhidefield2()"/>Newsletter
            <div id='hideablearea2' style='display:none'>
            <input type="checkbox" id="group_7_1" value="Choice 1" name="entry.7.group" />Choice 1
            <input type="checkbox" id="group_7_2" value="Choice 2" name="entry.7.group" />Choice 2

             <br><br></div>
<br><br>

             <input type="checkbox" class="checkbox" id="newsletter2" name="newsletter.group" onclick="showhidefield3()"/>Newsletter2
             <div id='hideablearea3' style='display:none'>

            <input type="checkbox" id="group_77_1" value="Choice 1" name="entry.77.group" />Choice 1  
            <input type="checkbox" id="group_77_2" value="Choice 2" name="entry.77.group" />Choice 2
           </div>
<br><br>           
            <input type="checkbox" class="checkbox" id="newsletter3" name="newsletter.group" onclick="showhidefield4()"/>Newsletter3
            <div id='hideablearea4' style='display:none'>

            <input type="checkbox" id="group_777_1" value="Choice 1" name="entry.777.group" />Choice 1
            <input type="checkbox" id="group_777_2" value="Choice 2" name="entry.777.group" />Choice 2   

      </div>
      </div>

            <p></p>
    <input type="hidden" name="pageNumber" value="0"> 
    <input type="hidden" name="backupCache" value=""> 


  <div class="ss-item ss-navigate"><div class="ss-form-entry"> 
  <p>
    <input name="pageNumber" type="hidden" value="0" />
    <input name="backupCache" type="hidden" />
    <input name="submit" type="submit" value="Submit Form"/> 

1 个答案:

答案 0 :(得分:1)

听起来你需要定制自己的“必需”规则的味道。看看这个:
http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression

它应该引导你走正确的道路。