简体   繁体   English

Bootstrap 多选搜索清除按钮单击事件不起作用

[英]Bootstrap Multi select search clear button click event not working

I am using bootstrap multi-selector with search enabled option.我正在使用带有搜索启用选项的引导多选择器。

在此处输入图像描述

I am trying to unselect all selected items on click of X button.我试图在单击 X 按钮时取消选择所有选定的项目。 But i am not able to handle click event for this button.但我无法处理此按钮的点击事件。

Please help or suggest a better way to do this.请帮助或建议更好的方法来做到这一点。

Here is sample code:这是示例代码:

<select id="multiselect-users" name="users[]" multiple="multiple">
    <option value="1">User1</option>
    <option value="2">User2</option>
</select>

<script type="text/javascript">
 $('#multiselect-users').multiselect({
    enableFiltering: true
 });
  // .multiselect-clear-filter is X button class. This button is part of bootstrape-multiselect if you enable filter
 $(document).on('click', '.multiselect-clear-filter', function(){

    // not working :(
    alert('test');
 });
</script>

在此处输入图像描述

I want to unselect all selected items on click of this X button.我想在单击此 X 按钮时取消选择所有选定的项目。

What you a really expecting is that when you click on a select option an alert is displayed.您真正期望的是,当您单击一个select option时,会显示一个警报。

But the name of this event is not called 'click' is called 'changed':但是这个事件的名字不叫'click'而是叫'changed':

 $(document).on('change', '#multiselect-users', function(){ // woking:) alert('test'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select id="multiselect-users" name="users[]" multiple="multiple"> <option value="1">User1</option> <option value="2">User2</option> </select>

Also, you should add this event to the select tag not to .multiselect-clear-filter .此外,您应该将此事件添加到select标签而不是.multiselect-clear-filter

I have to say something about the:我不得不说一些关于:

 $('#multiselect-users').multiselect({
    enableFiltering: true
 });

part.部分。 And is that change event doesn't work when this is enabled.启用此功能后, change事件是否不起作用。 But I think maybe this function is from another library.但我想也许这个函数来自另一个库。

Hope this helps!希望这可以帮助!

Hi You can try this option also您好您也可以试试这个选项

 $('#multiselect-users').multiselect({
        includeFilterClearBtn:true
 });

Try following code:试试下面的代码:

$('.multiselect-clear-filter').click(function () {
    $('#myDropDown option:selected').each(function () {
        $(this).prop('selected', false);
    })
    $('#myDropDown').multiselect('refresh');
});

NOTE: Do not include this inside $(document).Ready()注意:不要将其包含在$(document).Ready()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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