简体   繁体   中英

Limit multiselect amount based on value

There are quiet a few javascript scripts that allows me to limit the selection of multiselect checkboxes to a certain number. Ie I have a list of 4 answers but im only allowed to check 2.

However im looking for something that limits them based on the value of the question. ie i have a list of 4 answers. I can select two value 1 questions or i can select one value 2 question.

Anyone has the answer to this questions? Thx

you can use 'Change' function

<div class="checkbox-levels">
      <p><strong>Which level would you like? (Select 2 Levels)</strong></p>
      <input class="single-checkbox"type="checkbox" name="question" value="1">Level 1<br>
      <input class="single-checkbox" type="checkbox" name="question" value="2">Level 2<br>
      <input class="single-checkbox" type="checkbox" name="question" value="3">Level 3<br>
      <input class="single-checkbox" type="checkbox" name="question" value="4">Level 4<br>
    </div>


<script>
var limit = 2;
$('input.single-checkbox').on('change', function(evt) {
   if($(this).siblings(':checked').length >= limit) {
   this.checked = false;
   alert("allowed only 2");
 }
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM