2

複数のチェックボックスがあるフォームがあります。フォームには 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_2etc は、私が使用する CMS によって生成されます。

4

5 に答える 5

5

試してください(マークアップがフィドルのものと一致する場合)

jQuery(function(){
    var max = 3;
    var checkboxes = jQuery('input[type="checkbox"]');

    checkboxes.click(function(){
        var $this = $(this);
        var set = $this.add($this.prevUntil('label')).add($this.nextUntil(':not(:checkbox)'));
        var current = set.filter(':checked').length;
        return current <= max;
    });
});

デモ:フィドル

更新:
チェックボックスの名前があるので

jQuery(function(){
    var max = 3;
    var checkboxes = jQuery('input[type="checkbox"]');

    checkboxes.click(function(){
        var $this = $(this);
        var set = checkboxes.filter('[name="'+ this.name +'"]')
        var current = set.filter(':checked').length;
        return current <= max;
    });
});

デモ:フィドル

于 2013-07-17T06:28:56.753 に答える
2

jQuery

$("input[name=chk]").change(function(){
    var max= 3;
    if( $("input[name=chk]:checked").length == max ){
        $("input[name=chk]").attr('disabled', 'disabled');
        $("input[name=chk]:checked").removeAttr('disabled');
    }else{
         $("input[name=chk]").removeAttr('disabled');
    }
});

HTML

<input type="checkbox" name"chk" value="A"/>A
<input type="checkbox" name"chk" value="B"/>B
<input type="checkbox" name"chk" value="C"/>C
<input type="checkbox" name"chk" value="D"/>D
<input type="checkbox" name"chk" value="E"/>E
<input type="checkbox" name"chk" value="F"/>F
<input type="checkbox" name"chk" value="F"/>G

フィドルの例はこちら

于 2013-07-17T06:43:54.917 に答える
1

これを試して:

jQuery(function(){
   var max = 3;
   var categories = jQuery('label'); // use better selector for categories

   categories.each(function(){
       var checkboxes = $(this).find('input[type="checkbox"]');
       checkboxes.change(function(){
            var current = checkboxes.filter(':checked').length;
            checkboxes.filter(':not(:checked)').prop('disabled', current >= max);
        });
   });
});
于 2013-07-17T06:27:12.177 に答える
1

関連するすべてのチェックボックスにクラスを追加し、カテゴリのチェックボックスをチェックするときにクラスセレクターを使用するだけです. 例えば:

HTML

<label>Cat 1</label>
<input type='checkbox' class='cat1' />
<input type='checkbox' class='cat1' />
<input type='checkbox' class='cat1' />

<label>Cat 2</label>
<input type='checkbox' class='cat2' />
<input type='checkbox' class='cat2' />
<input type='checkbox' class='cat2' />
...

JS

jQuery(function(){
   var max = 3;

   var cat1_checkboxes = jQuery('input.cat1[type="checkbox"]');
   var cat2_checkboxes = jQuery('input.cat2[type="checkbox"]');
   var cat3_checkboxes = jQuery('input.cat3[type="checkbox"]');


   cat1_checkboxes.change(function(){
      var current = cat1_checkboxes.filter(':checked').length;
       checkboxes.filter(':not(:checked)').prop('disabled', current >= max);
   });

   cat2_checkboxes.change(function(){
      var current = cat2_checkboxes.filter(':checked').length;
       checkboxes.filter(':not(:checked)').prop('disabled', current >= max);
   });

   cat3_checkboxes.change(function(){
      var current = cat3_checkboxes.filter(':checked').length;
       checkboxes.filter(':not(:checked)').prop('disabled', current >= max);
   });

});
于 2013-07-17T06:31:22.100 に答える