jquery - Trigger an action after selection select2 -
i using select2 library search.
there way trigger action after selecting search result? e.g. open popup, or simple js alert.
$("#e6").select2({ placeholder: "enter item id please", minimuminputlength: 1, ajax: { // instead of writing function execute request use select2's convenient helper url: "index.php?r=sia/searchresults", datatype: 'jsonp', quietmillis: 3000, data: function (term, page) { return { q: term, // search term page_limit: 10, id: 10 }; }, results: function (data, page) { // parse results format expected select2. // since using custom formatting functions not need alter remote json data return {results: data}; }, }, formatresult: movieformatresult, // omitted brevity, see source of page formatselection: movieformatselection, // omitted brevity, see source of page dropdowncssclass: "bigdrop", // apply css makes dropdown taller escapemarkup: function (m) { return m; } // not want escape markup since displaying html in results });
see events section in documentation or further details section on events.
depending on version, 1 of snippets below should give event want, alternatively replace "select2-selecting" "change".
version 4.0 +
events in format: select2:selecting
(instead of select2-selecting
)
thanks snakey notification has changed of 4.0
$('#yourselect').on("select2:selecting", function(e) { // happen });
version before 4.0
$('#yourselect').on("select2-selecting", function(e) { // happen });
just clarify, documentation select2-selecting
reads:
select2-selecting fired when choice being selected in dropdown, before modification has been made selection. event used allow user reject selection calling event.preventdefault()
whereas change has:
change fired when selection changed.
so change
may more appropriate needs, depending on whether want selection complete , event, or potentially block change.
Comments
Post a Comment