複数のチェックボックスがあるフォームがあります。フォームには 3 つのカテゴリのチェックボックスがあります。カテゴリごとに最大 3 つのチェックボックスに制限する必要があります。
このスクリプトを使用しましたが、フォームごとに 3 つのチェックボックスに制限されています。
jQuery(function(){
var max = 3;
var checkboxes = jQuery('input[type="checkbox"]');
checkboxes.change(function(){
var current = checkboxes.filter(':checked').length;
checkboxes.filter(':not(:checked)').prop('disabled', current >= max);
});
});
上記のスクリプトを変更して、1 つのフォーム内のカテゴリごとに 3 つのチェックボックスを制限するにはどうすればよいですか?
HTML の例:
<!-- FORM CODE -->
<!-- Start Categories -->
<label>Cat 1</label>
<!-- Checkboxes for Cat 1 - Max of Three in Category -->
<input type="checkbox" value="Option 1" id="CAT_Custom_365571_0" name="CAT_Custom_365571" />Creative<br />
<input type="checkbox" value="Option 2" id="CAT_Custom_365571_1" name="CAT_Custom_365571" />Euphoric<br />
<input type="checkbox" value="Option 3" id="CAT_Custom_365571_2" name="CAT_Custom_365571" />Uplifted<br />
<!-- More checkboxes -->
<label>Cat 2</label>
<!-- Checkboxes for Cat 2 - Max of Three in Category -->
<input type="checkbox" value="Option 1" id="CAT_Custom_365572_0" name="CAT_Custom_365572" />Option 1<br />
<input type="checkbox" value="Option 2" id="CAT_Custom_365572_1" name="CAT_Custom_365572" />Option 2<br />
<input type="checkbox" value="Option 3" id="CAT_Custom_365572_2" name="CAT_Custom_365572" />Option 3<br />
<!-- More checkboxes -->
<label>Cat 3</label>
<!-- Checkboxes for Cat 3 - Max of Three in Category -->
<input type="checkbox" value="Option 1" id="CAT_Custom_365573_0" name="CAT_Custom_365573" />Option 1<br />
<input type="checkbox" value="Option 2" id="CAT_Custom_365573_1" name="CAT_Custom_365573" />Option 2<br />
<input type="checkbox" value="Option 3" id="CAT_Custom_365573_2" name="CAT_Custom_365573" />Option 3<br />
<!-- More checkboxes -->
<!-- MORE FORM CODE -->
注: CAT_Custom_365571_2
etc は、私が使用する CMS によって生成されます。