Quick way to clear all selections on a multiselect enabled <select> with jQuery?

后端 未结 13 2802
半阙折子戏
半阙折子戏 2020-12-09 07:52

Do I have to iterate through ALL the and set remove the \'selected\' attribute or is there a better way?

13条回答
  •  再見小時候
    2020-12-09 07:56

    You can pass an empty array to unselect all the selection. Here goes an example. From documentation

    val() allows you to pass an array of element values. This is useful when working on a jQuery object containing elements like , , and s inside of a . In this case, the inputs and the options having a value that matches one of the elements of the array will be checked or selected while those having a value that doesn't match one of the elements of the array will be unchecked or unselected, depending on the type.

    $('.clearAll').on('click', function() {
      $('.dropdown').val([]);
    });
    
    
    
    
    

提交回复
热议问题