select2 noresult捕获输入(select2 noresult catch the input) 如何捕获用户输入并将其传递给创建文本字段。

当前代码显示undefined 。

并且新术语也没有添加到列表中,警报似乎无法正常工作。

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

$(“#mySel”)。select2({width:'240px',allowClear:true,“language”:{“noResults”:function(term){/ *定制no matches输出* / return“Create”}} ,

escapeMarkup: function (markup) { return markup; }

}().parent()。find('。select2-with-searchbox')。on('click','#addNew',function(){/ * add new term * / var newTerm = $('#newTerm ').val(); alert('adding:'+ newTerm); $(''+ newTerm +'')。appendTo('#mySel'); $('#mySel')。select2('val',newTerm ); //选择新术语$(“#mySel”)。select2('close'); //关闭下拉列表})

});

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

How do I catch the user input and pass it to the create text field.

Current code shows undefined.

And the new term also not added to the list, the alert seem like not working.

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

$("#mySel").select2({ width:'240px', allowClear:true, "language": { "noResults": function(term) { /* customize the no matches output */ return "Create" } },

escapeMarkup: function (markup) { return markup; }

}) .parent().find('.select2-with-searchbox').on('click','#addNew',function(){ /* add the new term */ var newTerm = $('#newTerm').val(); alert('adding:'+newTerm); $(''+newTerm+'').appendTo('#mySel'); $('#mySel').select2('val',newTerm); // select the new term $("#mySel").select2('close'); // close the dropdown })

});

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

最满意答案

根据select2文档 ,您可以使用“tags”选项来实现您想要做的事情。 Select2已经为你做到了这一点:

“如果将标签选项传递给Select2,如果用户在搜索框中输入任何尚未存在的内容,它将显示在顶部,用户可以选择它。”

Per the select2 documentation, you can use the "tags" option to achieve what you're trying to do. Select2 already does that for you:

"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."

select2 noresult捕获输入(select2 noresult catch the input) 如何捕获用户输入并将其传递给创建文本字段。

当前代码显示undefined 。

并且新术语也没有添加到列表中,警报似乎无法正常工作。

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

$(“#mySel”)。select2({width:'240px',allowClear:true,“language”:{“noResults”:function(term){/ *定制no matches输出* / return“Create”}} ,

escapeMarkup: function (markup) { return markup; }

}().parent()。find('。select2-with-searchbox')。on('click','#addNew',function(){/ * add new term * / var newTerm = $('#newTerm ').val(); alert('adding:'+ newTerm); $(''+ newTerm +'')。appendTo('#mySel'); $('#mySel')。select2('val',newTerm ); //选择新术语$(“#mySel”)。select2('close'); //关闭下拉列表})

});

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

How do I catch the user input and pass it to the create text field.

Current code shows undefined.

And the new term also not added to the list, the alert seem like not working.

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

$("#mySel").select2({ width:'240px', allowClear:true, "language": { "noResults": function(term) { /* customize the no matches output */ return "Create" } },

escapeMarkup: function (markup) { return markup; }

}) .parent().find('.select2-with-searchbox').on('click','#addNew',function(){ /* add the new term */ var newTerm = $('#newTerm').val(); alert('adding:'+newTerm); $(''+newTerm+'').appendTo('#mySel'); $('#mySel').select2('val',newTerm); // select the new term $("#mySel").select2('close'); // close the dropdown })

});

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

最满意答案

根据select2文档 ,您可以使用“tags”选项来实现您想要做的事情。 Select2已经为你做到了这一点:

“如果将标签选项传递给Select2,如果用户在搜索框中输入任何尚未存在的内容,它将显示在顶部,用户可以选择它。”

Per the select2 documentation, you can use the "tags" option to achieve what you're trying to do. Select2 already does that for you:

"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."