This will be done, although there is a brief “flash” of choice (you may need to show <span>under <select>to say something like the maximum choice reached).
$('#mySelect').click(function(e) {
if ($('option:selected',this).length > 5) {
$(e.target).removeAttr('selected');
}
});
, .
EDIT:
, , , , IE ( IE <select>), , IE 6 Firefox 3.5.3, . ,
(function($) {
$.fn.maxLimitSelect = function(max) {
if (!max || !/^\d+$/.test(max)) return this;
return this.each(function() {
$(this).bind("click change keypress",{ max: max },function(event) {
var options = $('option', this);
var max = event.data.max;
var selected = $('option:selected', this);
var indexes;
if (selected.length === max) {
indexes = $.map(options, function(e, i) {
if(e.selected) return i;
});
$.data(this, "indexes", indexes);
}
else if (selected.length > max) {
indexes = $.data(this, "indexes");
options
.removeAttr('selected')
.each(function() {
var $this = $(this);
if ($.inArray($this.prevAll().length, indexes) !== -1) {
$this.attr('selected','selected');
}
});
}
});
});
}
})(jQuery);
$('#mySelect').maxLimitSelect(5);
, . IE Firefox, , IE 6 Ctrl Space , Shift . .