简体   繁体   English

当您处于“标记”模式时,如何初始化jquery select2项目?

[英]How do you initialize jquery select2 items when you are in “tagging” mode?

I am trying to figure out how to initialize a jquery select2 item when i am in tagging mode (as i want to allow users to enter new items: 我试图弄清楚当我处于标记模式时如何初始化一个jQuery select2项目 (因为我想允许用户输入新项目:

I see this example: 我看到这个例子:

 $("#e12").select2({tags:["red", "green", "blue"]});

which works fine but I need to instantiate it with some entries already added. 它工作正常,但我需要使用一些已经添加的条目来实例化它。

but if i want to initialize with some existing items on page load, i tried doing something like this: 但是,如果我想使用页面加载中的一些现有项目进行初始化,我尝试执行以下操作:

   var existingEmailAddresses= ["joe@abc.com","bill@abc.com"];

   $("#e12").select2({
            width: "600px",
            multiple: true,
            tags:["A", "B", "C"],
            initSelection: function (element, callback) {
                callback(existingEmailAddresses);
            }
        });

but that doesn't seem to be working. 但这似乎不起作用。

How can you initialize entrying when you are using tagging mode? 使用标记模式时如何初始化输入?

Edit 编辑

I also want to see if there is any hook to validate new tags added. 我也想看看是否有任何钩子来验证添加的新标签。 In my case I want to make sure they are valid email addresses. 就我而言,我想确保它们是有效的电子邮件地址。

Try 尝试

var existingEmailAddresses = ["joe@abc.com", "bill@abc.com"];

$("#e12").val(existingEmailAddresses).select2({
    width: "600px",
    multiple: true,
    tags: ["A", "B", "C"]
});

Demo: Fiddle 演示: 小提琴

In Select2 version 4.x, this got changed. 在Select2版本4.x中,此更改。 Select2 is applied to a "select" tags instead of using the initSelection method, it uses a data adaptor. Select2应用于“ select”标签,而不是使用initSelection方法,它使用数据适配器。 More info here https://select2.github.io/announcements-4.0.html 更多信息在这里https://select2.github.io/announcements-4.0.html

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

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