Javascript 如何调用;如果没有搜索结果“;在select2上?

Javascript 如何调用;如果没有搜索结果“;在select2上?,javascript,jquery,jquery-select2,Javascript,Jquery,Jquery Select2,你好 如何在select2上调用“无搜索结果”?例如,我想将事件侦听器添加到我的select2文本框中,但仅当没有找到结果时 以下是我的js代码: $("#textBoxId").select2({ placeholder: 'Select a product', formatResult: productFormatResult, formatSelection: productFormatSelection, dropdownClass: 'bigdrop',

你好

如何在select2上调用“无搜索结果”?例如,我想将事件侦听器添加到我的select2文本框中,但仅当没有找到结果时

以下是我的js代码:

$("#textBoxId").select2({
    placeholder: 'Select a product',
    formatResult: productFormatResult,
    formatSelection: productFormatSelection,
    dropdownClass: 'bigdrop',
    escapeMarkup: function(m) { return m; },
    formatNoMatches: function( term ) {
    return "<li class='select2-no-results'>"+"No results found.<button class='btn btn-success pull-right btn-xs' onClick='modal()'>Add New Item</button></li>";
    },
    minimumInputLength:1,
    ajax: {
        url: '/api/productSearch',
        dataType: 'json',
        data: function(term, page) {
            return {
                q: term
            };  
        },  
        results: function(data, page) {
            return {results:data};
        }   
    }   
});
$(“#textBoxId”)。选择2({
占位符:“选择产品”,
formatResult:productFormatResult,
formatSelection:productFormatSelection,
dropdownClass:“bigdrop”,
转义标记:函数(m){return m;},
formatNoMatches:函数(术语){
返回“
  • ”+“未找到任何结果。添加新项”
  • ”; }, 最小输入长度:1, 阿贾克斯:{ url:“/api/productSearch”, 数据类型:“json”, 数据:功能(术语,第页){ 返回{ 问:任期 }; }, 结果:功能(数据、页面){ 返回{结果:数据}; } } });
    我试着加上:

    formatNoMatches: function( term ) {
          $('.select2-input').on('keyup', function(e) {
             if(e.keyCode === 13) 
               {
                $("#modalAdd").modal();
               }
          });
        return "<li class='select2-no-results'>"+"No results found.<button class='btn btn-success pull-right btn-xs' onClick='modal()'>Add New Item</button></li>";
        },
    
    formatNoMatches:函数(术语){
    $('.select2 input')。打开('keyup',函数(e){
    如果(如keyCode===13)
    {
    $(“modalAdd”).modal();
    }
    });
    返回“
  • ”+“未找到任何结果。添加新项”
  • ”; },
    它工作了,当我按“回车键”时模式显示,但即使我不搜索任何东西,当我按回车键时,模式仍然显示


    我想达到的是,当“未找到结果”时,我只需按“回车”键,而不是单击链接,即可打开模式表单。。非常感谢你的帮助!祝你今天愉快

    通过搜索一些备选方案,我找到了这段代码,在调用模式后,我只需解除键控侦听器事件的绑定,这里是我的代码:

        formatNoMatches: function( term ) {
          var flag = 1;
          $('.select2-input').on('keyup', function(e) {
             if(e.keyCode === 13) 
               {
                $("#modalAdd").modal();
                $(".select2-input").unbind( "keyup" );
               }
          });
        return "<li class='select2-no-results'>"+"No results found.<button class='btn btn-success pull-right btn-xs' onClick='modal()'>Add New Item</button></li>";
        }
    
    formatNoMatches:函数(术语){
    var标志=1;
    $('.select2 input')。打开('keyup',函数(e){
    如果(如keyCode===13)
    {
    $(“modalAdd”).modal();
    $(“.select2输入”)。解除绑定(“键控”);
    }
    });
    返回“
  • ”+“未找到任何结果。添加新项”
  • ”; }
    结果:函数(数据,页面)
    替换为
    处理结果:函数(数据,页面)
    这是工作

    还有“动态选项创建”