简体   繁体   English

select2 noresult捕获输入

[英]select2 noresult catch the input

  1. How do I catch the user input and pass it to the create text field. 如何捕获用户输入并将其传递给创建文本字段。

Current code shows undefined . 当前代码显示为undefined

  1. 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(){ $ .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" } }, $(“#mySel”)。select2({width:'240px',allowClear:true,“ language”:{“ noResults”:function(term){/ *自定义无匹配项输出* /返回“ 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 }) }).parent()。find('。select2-with-searchbox')。on('click','#addNew',function(){/ *添加新术语* / var newTerm = $('#newTerm ').val(); alert('adding:'+ newTerm); $(''+ newTerm +'')。appendTo('#mySel'); $('#mySel')。select2('val',newTerm ); //选择新术语$(“#mySel”)。select2('close'); //关闭下拉菜单})

    }); });

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

Per the select2 documentation , you can use the "tags" option to achieve what you're trying to do. 根据select2文档 ,您可以使用“标签”选项来实现您要执行的操作。 Select2 already does that for you: 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." “如果将标记选项传递给Select2,则如果用户在搜索框中键入尚不存在的任何内容,它将显示在顶部,并且用户可以选择它。”

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

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