I'm using 2 widgets that adds the selections and only allows the user to select 5 total between both dropdown widgets. Strangely once the 5 limit is reached, I can not uncheck the checkboxes! Anyone know why this is happening? I also noticed that if I say >5, the user can still choose 6 before is stopped. I'm having to use 4 to stop at 5?
js
$(document).ready(function(){ $(".multiselect").multiselect({ header:"Choose up to 5 areas", click:function(event,ui){if($(".multiselect").children(":checked").length >4){returnfalse;}}, selectedList:5});
Edit Let me start again as I've recently learned some knowledge that changed what I thought I knew.
Why can't I uncheck checkboxes
You can't uncheck the checkboxes because you are preventing the default action by returning false from the click event handler if there are more than 4 boxes that are checked.
This if statement evaluates to true if more than 4 checkboxes are checked. Since you don't allow checkboxes to be checked or unchecked, that means once 4 have been checked, they will always be checked (until page refresh or programmatically uncheck).
Why do I have to use 4 to stop at 5.
The reason for this isn't what I thought at first. I found out that when you click a checkbox, it's state is changed before entering the click event handler. However, if you return false from the click handler, it's state will revert to what it was previously.
What this means is that if you have 4 checkboxes checked, and you click a 5th checkbox, before you see the UI update with the 5th checked checkbox, programmatically, 5 checkboxes are checked. If you return false, this number will drop back to 4. If you return true, this number would stay at 5.
This code will only allow you to select 3 checkboxes at a time.
It checks if the clicked checkbox is not checked.. This means that the checkbox was previously checked. If the checkbox was checked previously, then we always want to allow the checkbox to be unchecked.
After that, it checks how many checkboxes are checked. I used 3 as the limit for how many checkboxes could be checked at a time. You could change (3) to (4) for your domain.
Here's your domain specific code:
$(document).ready(function(){ $(".multiselect").multiselect({ header:"Choose up to 5 areas", click:function(event, ui){if(!this.checked)returntrue;//Allow previously checked checkboxes to be unchecked.if($(".multiselect").children(":checked").length >4){//Only allow 4 checkboxes to be checked simultaneously.returnfalse;}}, selectedList:5});});
回答2:
You can do like this
$("select").multiselect({ selectedList:5, header:"Choose only Five items items!", click:function(e){if( $(this).multiselect("widget").find("input:checked").length >5){returnfalse;}}});
回答3:
The OP (user3191137) is using Eric Hynds jQuery UI MultiSelect Widget drop down selector. The "click" method built in to the API assigns $(this) values specifically tied to the unique 'widget' id of each MultiSelect instance on the page.
This causes a problem trying to count the overall number of checkboxes across two or more selector instances on the page. So a separate control is required which can keep track of the number of checkboxes checked overall.
I have added a message layer into my example just to help show the status during the selections.
The way I solved this issue was having a separate variable counting the number of checks called "ClikCount". Checking this value either allows a selection or removes the selection if more than the max of 5 in this case.