单个复选框检查多个表单

时间:2013-10-13 11:16:05

标签: jquery forms input checkbox

我在一个页面中有几个具有不同值的表单,这些表单的数量很可能会在一段时间内增加。现在我不会允许用户提交任何这些表单,直到他们选择一个复选框名称=“terms”,它只出现一个并且不会放在任何这些表单中。可能还是我必须将它放在每个表格中?

<input type="checkbox" name="terms" /> Agree to terms and conditions.

<form action="./action/studentMentorSignup.php" method="post" name="studentsForm">
<input type="hidden" value="145" name="mentorID" />
<button class="btn btn-warning" type="submit" name="submitRequest"><i class="icon-check"></i></button>
<a href="#myModal10" role="button" data-toggle="modal" class="btn btn-info"><i class="icon-info-sign"></i></a>
</form>

<form action="./action/studentMentorSignup.php" method="post" name="studentsForm">
<input type="hidden" value="244" name="mentorID" />
<button class="btn btn-warning" type="submit" name="submitRequest"><i class="icon-check"></i></button>
<a href="#myModal11" role="button" data-toggle="modal" class="btn btn-info"><i class="icon-info-sign"></i></a>
</form>

<form action="./action/studentMentorSignup.php" method="post" name="studentsForm">
<input type="hidden" value="477" name="mentorID" />
<button class="btn btn-warning" type="submit" name="submitRequest"><i class="icon-check"></i></button>
<a href="#myModal11" role="button" data-toggle="modal" class="btn btn-info"><i class="icon-info-sign"></i></a>
</form>

...

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

<input type="checkbox" id="terms"> Agree to terms and conditions.

Javascript:

 $(".forms").submit(function(e){
       if(!$("#terms").is(":checked")){
            //alert('You need to accept terms');
            e.preventDefault();
       }
    });

forms课程添加到表格中,例如<form class="forms"

答案 1 :(得分:1)

给复选框ID,以便您可以直接定位它,并将其检查条件作为布尔值进行测试:

<label for="terms">Agree to terms and conditions.</label>
<input id="agreefirst" type="checkbox" name="terms" /> 

<script>
     var isChecked = document.getElementById('agreefirst').checked;
     if ( isChecked ) 
         //Code..
</script>

这不一定是<form>标签,它仍然可以与其他标签保持偏离。

然后,一旦您提交了其他表单之一,您仍然可以对其进行测试,

<script>
     function validate( event ) {
         if ( !document.getElementById('agreefirst').checked ) {
              event.preventDefault();
              return false;
         }
     }
</script>

<form action="./action/studentMentorSignup.php" 
      onsubmit="validate( event )" 
      method="post" 
      name="studentsForm">