简体   繁体   中英

jQuery Multiselect - Select All Event Handler

I have a jQuery multiselect that has select all option checked by default. Whenever there's a change in the selected options I'm reloading the page data accordingly.

Everything works great except that the 'Select All' button click doesn't trigger onChange event and I'm not able to reload the data when that is clicked. I tried attaching event handlers to checkAll and selectAll as well but to no avail.

$("#testselect").multiselect({
        nonSelectedText: 'None',
        allSelectedText: 'All Selected',
        includeSelectAllOption: true,
        buttonWidth: '100%',
        checkAll: function () {
            alert("check all"); // Doesn't work
        },
        selectAll: function () {
            alert("select all"); // Doesn't work
        },
        onChange: function (option, checked, select) {
            alert("onchange") // Works but not for 'All Selected'
            // Do something
        }
});
$("#testselect").multiselect('selectAll', false);
$("#testselect").multiselect('updateButtonText');

Add Main Check Box:

<th>SELECT ALL<br/><input type="checkbox"  onClick="toggle(this)" /></th>

Add Script:

          <script>

          function toggle(source) {
           checkboxes = document.getElementsByName('checkbox[]');
           if(x=1)
           {
             for(var i=0, n=checkboxes.length;i<n;i++) {
               checkboxes[i].checked = source.checked;
             }}}



           </script>

Add Checkboxes You like to select by main:

<input name="checkbox[]"    type="checkbox" >

This works for me.

$(document).on('click', '.ms-selectall', function( event ){
        //your code
});

Instead of checkAll and selectAll , use onSelectAll and onDeselectAll event

Example :

$("#testselect").multiselect({
        nonSelectedText: 'None',
        allSelectedText: 'All Selected',
        includeSelectAllOption: true,
        buttonWidth: '100%',
        onChange: function (option, checked, select) {
            alert("onchange") // Works but not for 'All Selected'
        },
        onSelectAll: function () {
            console.log("call when select all");
        },
        onDeselectAll: function () {
            console.log("call when deselect all");
        }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM