2013-10-13 91 views
0

我有一個頁面內的幾個形式不同的值和這些形式的數量將最有可能增加的時間。現在我不會允許用戶提交任何這些表單,直到他們選擇複選框名稱=「條款」,該條目只出現一個,而不是放在任何這些表單中。可能還是我將不得不將它放在每一個表單中?單複選框檢查多種形式

<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
<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"

+0

謝謝,很好! –

+0

沒有注意到jQuery標籤! =)不錯的答案+1 – MackieeE

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">