0

1 つのページ内に異なる値を持つフォームはほとんどなく、これらのフォームの数はおそらく時間の経過とともに増加します。ここで、ユーザーがチェックボックス name="terms" を選択するまで、ユーザーがこれらのフォームを送信できないようにします。これは 1 つだけ表示され、それらのフォームのいずれにも配置されません。可能ですか、それともすべてのフォーム内に配置する必要がありますか?

<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 に答える