when click on close button and when click on sugge

2019-09-19 15:25发布

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:

enter image description here

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

enter image description here

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条回答
倾城 Initia
2楼-- · 2019-09-19 16:08

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'
查看更多
登录 后发表回答