Firing event when user clicks no matches found jquery plugin select2

笑着哭i 提交于 2019-12-05 18:49:59
technology_dreamer

Hope my solution helps with your problem:

*Select2 configuration

var $select2 = $('#select2_id').select2(
{
    //Your parameters
    formatNoMatches: Not_Found
});

*Error handling function

function Not_Found()
{
    var $not_found = '<div>Result not found. <a href="#" onclick="return myClick()">click here to event</a></div>';
    return $not_found;
}

*myClick() function

function myClick()
{
  alert('hello');
}

Note: In my code, this function needs to be at the top to avoid an error which I don't know why this happens.

Source: https://github.com/ivaynberg/select2/issues/276

Try to use on() like,

$('.select2-drop-active').on('click','li.select2-no-results',function(){
    alert('Hello');
});
CodeBox

try using .trigger()

   $(".select2-no-results").click(function()
                               {
                                  $("li.select2-no-results").trigger("click");
                               });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!