如何防止从select2中删除标签 [英] How to prevent deleting of tags from select2

查看:346
本文介绍了如何防止从select2中删除标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用tags:true创建了一个select2小部件.是否可以通过单击下拉菜单中的所选项目来防止标签被删除,从而只能通过单击标签中的叉号来删除标签?

I've created a select2 widget with tags:true. Is there a way to prevent the deletion of tags by click on the selected item in the drop down, so that the tag only can be removed by clicking the cross in the tag?

推荐答案

您可以使用事件select2:unselecting取消从商品中删除标签,例如

You can use the event select2:unselecting to cancel the tag deletion from the item, eg

$(".js-example-tags").select2({
  tags: true
}).on("select2:unselecting", function (e) {
    // check if originalEvent.currentTarget.className is "select2-results__option" (in other words if it was raised by a item in the dropdown)
    if ($(e.params.args.originalEvent.currentTarget).hasClass("select2-results__option")){
        e.preventDefault();
        // close the dropdown
        $(".js-example-tags").select2().trigger("close");
    }
});

JSFiddle演示

这篇关于如何防止从select2中删除标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆