when click on close button and when click on suggestions text with select2

喜夏-厌秋 提交于 2020-01-07 08:41:33

问题


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

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