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????
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.
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");
});
来源:https://stackoverflow.com/questions/19950299/firing-event-when-user-clicks-no-matches-found-jquery-plugin-select2