wenzhixin / multiple-select

A jQuery plugin to select multiple elements with checkboxes :)
http://multiple-select.wenzhixin.net.cn
MIT License
1.91k stars 651 forks source link

Changing onClick so that the implementation can control if a click succeeds #440

Closed mraymond closed 6 months ago

mraymond commented 5 years ago

By moving the onClick event call up and checking the return value, it enables users to prevent the box from being checked. This opens up new features, such as limiting the amount of boxes that can be checked, requested in #194

var count = 0;
var max = 2;
$("select").multipleSelect({
  onClick: function(view) {
    if(!view.checked && count+1 > max) {
      return false;
    }
    if(!view.checked) count++;
    else count--;
});
ghiscoding commented 6 months ago

I think this PR can be closed since onBeforeClick was added in PR #610 which can be used for this exact use case