问题
I want check if a user click on select2 close button or only it's triggered a change event. This is my select2 input:
<%= hidden_field_tag :query, params[:query], :id => "query_txt" %>
On my coffeescript file:
$('#query_txt').select2(
#select2 options here, ajax, initSelection...etc
#
#
#
$("#query_txt").on "change", (e) ->
if ($(".select2-search-choice-close").click ->)
console.log "click on close"
)
The problem is that if I click on suggestions text I can see the text "click on close" on my console.
In other words, If you click on x/remove button as in this image:

the same behavior happens if you click in suggestions text as in this image:

You can see a example code in http://jsfiddle.net/nqWNJ/9/
My question is how I can know when a user clicks on suggestions text or click on the close button.
Thank you
回答1:
you need to add another event that will trigger on click.
$queryTxt = $('#query_txt') #cache object
$queryTxt.select2(
#select2 options here, ajax, initSelection...etc
#
#
#
)
$queryTxt.on 'change', (obj) ->
console.log 'clicked on close' if obj.removed
console.log 'change was triggered'
来源:https://stackoverflow.com/questions/15586879/when-click-on-close-button-and-when-click-on-suggestions-text-with-select2