如何在Jquery中的Select2元素上检测单击事件处理程序?

时间:2019-02-06 10:57:11

标签: javascript jquery-select2

我试图检测单击的元素是否为select2,并根据需要执行其他操作,而不仅仅是打开下拉列表。但这并不成功,因为我单击了select2元素,什么也没发生。

$(document).ready(function() {
  $('select').select2();

  $('body').on('click', function(e) {
    if ($(this).data('select2')) {
      console.log('select2 element was just clicked');
    } else {
      console.log('you are not clicking on select2');
    }
  });

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.4/select2.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.4/select2.css" />

<select style="width:300px" id="select">
  <optgroup label="Alaskan/Hawaiian Time Zone">
    <option value="AK">Alaska</option>
    <option value="HI">Hawaii</option>
  </optgroup>
  <optgroup label="Pacific Time Zone">
    <option value="CA">California</option>
    <option value="NV">Nevada</option>
    <option value="OR">Oregon</option>
    <option value="WA">Washington</option>
  </optgroup>
  <optgroup label="Mountain Time Zone">
    <option value="AZ">Arizona</option>
    <option value="CO">Colorado</option>
    <option value="ID">Idaho</option>
    <option value="MT">Montana</option>
    <option value="NE">Nebraska</option>
    <option value="NM">New Mexico</option>
    <option value="ND">North Dakota</option>
    <option value="UT">Utah</option>
    <option value="WY">Wyoming</option>
  </optgroup>
  <optgroup label="Central Time Zone">
    <option value="AL">Alabama</option>
    <option value="AR">Arkansas</option>
    <option value="IL">Illinois</option>
    <option value="IA">Iowa</option>
    <option value="KS">Kansas</option>
    <option value="KY">Kentucky</option>
    <option value="LA">Louisiana</option>
    <option value="MN">Minnesota</option>
    <option value="MS">Mississippi</option>
    <option value="MO">Missouri</option>
    <option value="OK">Oklahoma</option>
    <option value="SD">South Dakota</option>
    <option value="TX">Texas</option>
    <option value="TN">Tennessee</option>
    <option value="WI">Wisconsin</option>
  </optgroup>
  <optgroup label="Eastern Time Zone">
    <option value="CT">Connecticut</option>
    <option value="DE">Delaware</option>
    <option value="FL">Florida</option>
    <option value="GA">Georgia</option>
    <option value="IN">Indiana</option>
    <option value="ME">Maine</option>
    <option value="MD">Maryland</option>
    <option value="MA">Massachusetts</option>
    <option value="MI">Michigan</option>
    <option value="NH">New Hampshire</option>
    <option value="NJ">New Jersey</option>
    <option value="NY">New York</option>
    <option value="NC">North Carolina</option>
    <option value="OH">Ohio</option>
    <option value="PA">Pennsylvania</option>
    <option value="RI">Rhode Island</option>
    <option value="SC">South Carolina</option>
    <option value="VT">Vermont</option>
    <option value="VA">Virginia</option>
    <option value="WV">West Virginia</option>
  </optgroup>
</select>

我不知道为什么select2没有收到点击事件。是否有任何解决方法存档,因为单击select2时我需要执行更多操作?非常感谢。

1 个答案:

答案 0 :(得分:0)

这样的事情怎么样? 这是另一个示例:https://jsfiddle.net/f8q2by55/ http://jsfiddle.net/6jaodjzq/

如本答案所述:Attach click-event to element in .select2-result

  

因为Select2库阻止了弹出菜单上的任何单击事件,所以您无法将事件直接绑定到.info。但是您可以重新定义onSelect方法并将所需的任何代码放置在那里。

public void f1(A a){
    if(a instanceof B) {
        B b = (B)a;
        b.f1(b);
    } else {
        System.out.println("Nothing");
    }
}
$(document).ready(function() {
  let selector = $('#select').select2()
     selector.onSelect = (function(fn) {
        return function(data, options) {
            console.log('selected');
        }
    })(selector.onSelect);
})