0

我有一页内的表格很少,具有不同的值,并且这些表格的数量很可能会随着时间的推移而增加。现在我不会允许用户提交任何这些表单,直到他们选择一个复选框 name="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>

……

感谢帮助。

4

2 回答 2

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"

于 2013-10-13T11:22:47.893 回答
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">
于 2013-10-13T11:19:07.423 回答