下記のHTMLで書いたチェックボックスの選択可能個数の上限を3こにするためのコードをjavaascriptで書いたのですが、機能しません。解決策をご存知の方いらっしゃいましたら教えていただきたいです。
javascript
1function click_cb(){ 2 //チェックカウント用変数 3 var check_count = 0; 4 5 // 箇所チェック数カウント 6 $(".group input").each(function(){ 7 var parent_checkbox = $(this).children(".group input"); 8 9 if(parent_checkbox.prop('checked')){ 10 check_count = check_count+1; 11 } 12 }); 13 14 // 0個のとき(チェックがすべて外れたとき) 15 16 // 3個以上の時(チェック可能上限数) 17 if(check_count > 2){ 18 $(".group input").each(function(){ 19 // チェックされていないチェックボックスをロックする 20 if(!$(this).children(".group input").prop('checked')){ 21 $(this).children(".group input").prop('disabled',true); 22 $(this).addClass("locked"); 23 } 24 }); 25 }else{ 26 $("..group input").each(function(){ 27 // チェックされていないチェックボックスを選択可能にする 28 if(!$(this).children(".group input").prop('checked')){ 29 $(this).children(".group input").prop('disabled',false); 30 $(this).removeClass("locked"); 31 } 32 }); 33 } 34 35 return false; 36} 37
HTML
1<div class= "group3"> 2 <table border="" width="840px" height="700"> 3 <tr><th colspan=6 height="10%">グループⅢ</th></tr> 4 <tr width="16%" height="10%"> 5 <th>A難度</th><th>B難度</th><th>C難度</th><th> 6 D難度</th><th>E難度</th><th>F難度</th> 7 </tr> 8 <tr align="center"> 9 <td> 10 <label><input type="checkbox" class="G3" value="0.1" onclick = "click_cb();">前宙</label> 11 </td> 12 <td> 13 <label><input type="checkbox" class="G3" value="0.2" onclick = "click_cb();">前宙</label> 14 </td> 15 <td> 16 <label><input type="checkbox" class="G3" value="0.3" onclick = "click_cb();">前宙</label> 17 </td> 18 <td> 19 <label><input type="checkbox" class="G3" value="0.4" onclick = "click_cb();">前宙</label> 20 </td> 21 <td> 22 <label><input type="checkbox" class="G3" value="0.5" onclick = "click_cb();">前宙</label> 23 </td> 24 <td> 25 <label><input type="checkbox" class="G3" value="0.6" onclick = "click_cb();">前宙</label> 26 </td> 27 </tr> 28 <tr> 29 <td colspan="6" align="center"> 30 合計点:<span id="total_3">0</span>点 31 </td> 32 </tr> 33 </table> 34</div>
HTML には class="group" がないのですが、どこにあるのでしょうか?
回答2件
あなたの回答
tips
プレビュー