Javascript 选择2事件以创建新标记

Javascript 选择2事件以创建新标记,javascript,jquery,jquery-select2,Javascript,Jquery,Jquery Select2,我使用jQuery(v4)插件作为标记选择器 我想要监听在select元素中创建新标记的时间,并发出ajax请求来存储新标记。我发现有createTag事件,但似乎每次在select2元素中输入字母时都会触发该事件。如我的小提琴所示: 是否有类似的事件仅在新标记输入完毕时触发?也就是说,它被一个灰色框所包围。不幸的是,我找不到任何本地方法。但如果你对简单的“变通办法”感兴趣,也许这会让你更接近: $('.select2').select2({ tags: true, tokenS

我使用jQuery(v4)插件作为标记选择器

我想要监听在select元素中创建新标记的时间,并发出ajax请求来存储新标记。我发现有
createTag
事件,但似乎每次在select2元素中输入字母时都会触发该事件。如我的小提琴所示:

是否有类似的事件仅在新标记输入完毕时触发?也就是说,它被一个灰色框所包围。

不幸的是,我找不到任何本地方法。但如果你对简单的“变通办法”感兴趣,也许这会让你更接近:

$('.select2').select2({
    tags: true,
    tokenSeparators: [",", " "],
    createTag: function (tag) {
        return {
            id: tag.term,
            text: tag.term,
            // add indicator:
            isNew : true
        };
    }
}).on("select2:select", function(e) {
    if(e.params.data.isNew){
        // append the new option element prenamently:
        $(this).find('[value="'+e.params.data.id+'"]').replaceWith('<option selected value="'+e.params.data.id+'">'+e.params.data.text+'</option>');
        // store the new tag:
        $.ajax({
            // ... 
        });
    }
});

另一种解决方法。只需将其插入开头:

           }).on('select2:selecting', function (evt) {

             var stringOriginal = (function (value) {
                // creation of new tag
                if (!_.isString(value)) {
                    return  value.html();
                }
                // picking existing
                return value;
            })(evt.params.args.data.text);
            ........
它依赖于下划线.js来检查它是否为字符串。您可以用任何喜欢的方法替换u.isString方法


它使用了这样一个事实,即创建新术语时,它始终是一个对象。

创建新标记时,唯一适用于我的事件侦听器是:

.on("select2:close", function() {
 (my code)
})

这是为新标签和从列表中选择而触发的<代码>更改,
select2:select
select2:selection
和其他任何操作都不起作用。

根据事件的参数的差异,再进行一次简单的检查

当我处理这种情况时,我看到了这种差异;创建新元素时,事件args数据没有元素对象,但在选择已可用选项时它存在

.on('select2:selecting', function (e) {
   if (typeof e.params.args.data.element == 'undefined') {  
      // do a further check if the item created id is not empty..
       if( e.params.args.data.id != "" ){
          // code to be executed after new tag creation
       }
   }
 })

您不能只使用
:选择
事件吗?因此,每次选择某个对象时,都会得到如下结果:
$(选择器).on(“select2:select”,function(e){console.log(e.params.data);})。这确实不会在现有对象和新对象之间产生差异,但这只是一个开始。
createTag
不是一个事件,而是一个被覆盖的方法。看起来开发人员第二天在Gitlab上回答了您的问题()-如果您更新了您的问题或提供了答案,那就太好了。对于其他人,开发人员建议收听
select2:selected
事件;我发现一个
change
事件也被触发。愿编码之神保佑你@paul我如何检测标记是否被删除?我只希望当新标记被删除时,你可以在创建新标记时添加一个指示符(如类),只需在
:unselect
事件处理程序中检查该指示符即可。如果键入“aw”并按enter键选择“awesome”,则会创建一个新标记“aw”,将其添加到选择框中,并添加“awesome”。显然,在本例中,所需的行为只是添加“awesome”,而不是添加新标记。我想这就是@Sahan所解释的,但我想不出一种方法来防止这个问题。select2的作者(select2)的答案似乎使用了一种隐藏在文档中的不同方法来回答:为了区别于我使用的现有标记或新标记:
if(e.params.hasOwnProperty('originalSelect2Event'))&e.params.originalSelect2Event.data.isNew==true)
.on('select2:selecting', function (e) {
   if (typeof e.params.args.data.element == 'undefined') {  
      // do a further check if the item created id is not empty..
       if( e.params.args.data.id != "" ){
          // code to be executed after new tag creation
       }
   }
 })