jquery 删除选定项2前发出警报

vqlkdk9b  于 2023-02-03  发布在  jQuery
关注(0)|答案(1)|浏览(192)

我不知道如何使一个条件,将显示警报时,有人想将删除项目从select2。

$(document).ready(function () {
         $("#select_name").select2({
            multiple: true,
            maximumSelectionLength: 2,
            minimumInputLength: 2,
        }).on('select2:select', function (e) {
            $(this).val([]).trigger('change');
            $(this).val([e.params.data.id]).trigger("change");      
        });
    });
bvn4nwqk

bvn4nwqk1#

这里我的朋友,你只需要返回false,如果你不想删除这个项目,如果你想的话返回true。
选择2-事件:https://select2.org/programmatic-control/events

$(document).ready(function() {
    $('.example').select2({
       multiple: true,
       maximumSelectionLength: 2,
       minimumInputLength: 2,
    }).on('select2:unselecting', function (e) {
         alert('Opps. Removing item?')
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<select class="example" style="width: 100px;" name="state">
  <option value="one">Example 1</option>
  <option value="two">Example 2</option>
  <option value="three">Example 3</option>
</select>

相关问题