select2 noresult 捕获输入

select2 noresult catch the input

  1. 如何捕获用户输入并将其传递给创建文本字段。

当前代码显示 undefined

  1. 而且新词也没有添加到列表中,警报似乎不起作用。

    $.getScript('//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.full.js',function(){

    $("#mySel").select2({ 宽度:'240px', allowClear:true, "language":{ "noResults":函数(术语){ /* 自定义不匹配输出 */ return "Create" } },

    escapeMarkup: function (markup) {
        return markup;
    }
    

    }) .parent().find('.select2-with-searchbox').on('click','#addNew',function(){ /* 添加新词 */ var newTerm = $('#newTerm').val(); 警报('adding:'+newTerm); $(''+newTerm+'').appendTo('#mySel'); $('#mySel').select2('val',newTerm); // select 新术语 $("#mySel").select2('close'); // 关闭下拉框 })

    });

http://www.codeply.com/go/LxasHWd0rd

根据 select2 documentation,您可以使用 "tags" 选项来实现您想要做的事情。 Select2 已经为您做到了:

"If the tags option is passed into Select2, if a user types anything into the search box which doesn't already exist, it will be displayed at the top and the user will be able to select it."