1

我有来自同一页面的两个单独搜索表单的两个输入。一个有data-result-select-url属性,一个没有。

<input id="q_id_eq" class="foo" type="text" size="30" name="q[id_eq]" data-result-select-url="/products/(id)" tabindex="-1"></input>

<input id="q_id_eq" class="foo" type="text" size="30" name="q[id_eq]" tabindex="-1"></input>

我为这些输入编写了自己的 select2 插件。

$ = jQuery
$.fn.my_select2 = (args) ->
  ...
  select_url = $(this).data("result-select-url")
  if select_url?
    $(this).on "change", (e) ->
      ...

  $(this).select2 args

当我$().each()用来调用我的插件my_select2()时,这很好用。只有一个输入接收“更改”事件。

jQuery
  $('.foo').each ->
    $(this).my_select2()

但是,如果我使用 jQuery 隐式迭代调用 my_select2() ,则两个输入都会收到事件。

jQuery ->
  $('.foo').my_select2()

解释是什么?我查找了jQuery 文档each但找不到答案。

4

0 回答 0