Firing event when user clicks no matches found jquery plugin select2

百般思念 提交于 2019-12-07 15:48:37

问题


Is there a possible way to fire an event when user clicks the no matches found message in jquery plugin select2??? i need that event for my project i have tried this code but it seems not working

$('.select2-no-results').live('click',function(){
alert("Yes");
});

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

is there a possible solution for this????


回答1:


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




回答2:


Try to use on() like,

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



回答3:


try using .trigger()

   $(".select2-no-results").click(function()
                               {
                                  $("li.select2-no-results").trigger("click");
                               });


来源:https://stackoverflow.com/questions/19950299/firing-event-when-user-clicks-no-matches-found-jquery-plugin-select2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!