select2删除选中项,allowClear设置

在使用select2过程中,有时候需要删除我们选中的选项,如下图:


这时候就需要设置select2的allowClear属性了。

有两种方法:

第一种:

    直接用select2定义的一个class

        $(".select2-allow-clear").select2({
            allowClear: true,
            placeholder: placeholder,
            width: null
        });

在select标签上加上.select2-allow-clear 的class属性即可;

第二种:

    和上面其实是一样的,自定义选中select标签,自定义属性:

$('#select2Id').select2({
         placeholder: "请选择",
         placeholderOption: "first",
         allowClear: true
      });
但是一定要注意,必须添加placeholderOption: "first"属性,否则allowClear不生效


猜你喜欢

转载自blog.csdn.net/sayoko06/article/details/80326539