lundi 24 octobre 2016

Jquery Checkbox limit with a value that equals to the sum of 5

Ok, I have a jQuery that limits the amount of checkboxes that can be selected at one time. My problem is: I have 10 Checkboxes that have a value of 1. I also have 5 checkboxes that have a Value of 2. In Opecart, A product has a specific Value, so I cannot overwrite this value without breaking the system in place.

These 2 sets of checkboxes have there own limits assigned to them: Value 1 boxes have a limit of 5. Value 2 boxes have a limit of 2.

The sum they all add up to is 5. Is there any way of pairing these limitations up?

An Example of what I'm after: A User checks 3 Value 1 Checkboxes, the current value is 3. They can now, select 2 more value 1 checkboxes OR 1 value 2 checkbox to meet the sum of 5.

The Logic:

  • 5 Value 1 = 5
  • 3 Value 1 + 1 Value 2 = 5
  • 1 Value 1 + 2 Value 2 = 5

This is the Jquery used to limit the checkboxes

var limit = 5;
$('input.single-checkbox').on('change', function(evt) {
   if($(this).siblings(':checked').length >= limit) {
       this.checked = false;
   }
});

This is an extract of the product page, where Opencart defines its own default value in the checkbox.

input class="limit" type="checkbox" name="option[547][]" value="1218" id="option-value-1218">

You can see I cannot just replace this value as it serves a purpose in the fact it pulls that products from the page to the cart. I need to find a way of applying the logic I have stated without compromising the system.




Aucun commentaire:

Enregistrer un commentaire