Limit the number of selected options using the <code>onChange</code> option. The user may only select a maximum of 4 options. Then all other options are disabled.
</p>
<divclass="btn-group">
<selectid="example37"multiple="multiple">
<optionvalue="cheese">Cheese</option>
<optionvalue="tomatoes">Tomatoes</option>
<optionvalue="mozarella">Mozzarella</option>
<optionvalue="mushrooms">Mushrooms</option>
<optionvalue="pepperoni">Pepperoni</option>
<optionvalue="onions">Onions</option>
</select>
</div>
</td>
<td>
<preclass="linenums prettyprint">
<script type="text/javascript">
$(document).ready(function() {
$('#example37').multiselect({
onChange: function(option, checked) {
// Get selected options.
var selectedOptions = $('#example37 option:selected');
if (selectedOptions.length >= 4) {
// Disable all other checkboxes.
var nonSelectedOptions = $('#example37 option').filter(function() {
return !$(this).is(':selected');
});
var dropdown = $('#example37').siblings('.multiselect-container');
nonSelectedOptions.each(function() {
var input = $('input[value="' + $(this).val() + '"]');
input.prop('disabled', true);
input.parent('li').addClass('disabled');
});
}
else {
// Enable all checkboxes.
var dropdown = $('#example37').siblings('.multiselect-container');
$('#example37 option').each(function() {
var input = $('input[value="' + $(this).val() + '"]');
input.prop('disabled', false);
input.parent('li').addClass('disabled');
});
}
}
});
});
</script>
</pre>
</td>
</tr>
<tr>
<td>
<p>
Record the order the options are selected. When selecting an item an ordering number will be incremented and saved within the option