jQuery事件附加到对象数组

时间:2019-04-12 15:39:54

标签: javascript jquery arrays

我想将一个事件绑定到选择元素,其中将每个选择元素都转换为一个jquery-selectbox元素 (https://github.com/marcj/jquery-selectBox

 SELECT distinct(T0.distnumber) as 'Batch Number', 
 T0.itemcode, 
 T1.itemname, 
 T0.Indate as 'Admission Date', 
 CASE WHEN Datediff(day, T0.indate,GETDATE()) <=30 THEN T0.distnumber 
 END'<30Days',
 CASE WHEN DateDiff(day,T0.indate,GETDATE()) >= 30 AND 
 DateDiff(day,T0.indate,GETDATE()) < 60 THEN T0.distnumber END '30 to 60 
 Days',
 CASE WHEN DateDiff(day, T0.indate,GETDATE()) >= 60  AND 
 DateDiff(day,T0.indate,GETDATE()) < 90 THEN T0.distnumber END '60 to 90 
 Days',
 CASE WHEN Datediff(day, T0.indate,GETDATE()) >90 THEN T0.distnumber 
 END'>90Days'

 From OBTN T0
 left join OITM T1 on T0.itemcode = T1.itemcode

添加一个插件:https://plnkr.co/edit/L3dwtVl4rjn2BKBe43x5?p=preview

当我在选择框中选择一个选项时,上面绑定的jquery事件触发了那里存在的许多select元素。就我而言,我有3个选择元素,因此触发了三次。

我们非常感谢您的帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

这似乎是插件事件处理程序中的错误,请注意在每个事件上触发的索引。如果您阅读代码,则会在模糊等情况下触发。更改更可靠,但是索引仍然不正确。

let selB = $('.select').selectBox({});
selB.on('close.selectbox', function(el, triggerData) {
  console.log($(this).html(), triggerData);
  console.log($(this).index('.select'));
});
selB.on('change.selectbox', function(el, triggerData) {
  console.log($(this).html(), triggerData);
  console.log($(this).index('.select'));
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.selectbox/1.2.0/jquery.selectBox.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.selectbox/1.2.0/jquery.selectBox.min.js"></script>
<select class="select">
  <option>oneer0</option>
  <option>oneer1</option>
  <option>oneer2</option>
  <option>oneer3</option>
</select>
<select class="select">
  <option>Toneer0</option>
  <option>Toneer1</option>
  <option>Toneer2</option>
  <option>Toneer3</option>
  <option>Toneer4</option>
</select>
<select class="select">
  <option>T3oneer0</option>
  <option>T3oneer1</option>
  <option>T3oneer2</option>
  <option>T3oneer3/option>
    <option>T3oneer4</option>
</select>

答案 1 :(得分:-2)

根据插件文档,您使这个问题变得过于复杂

尝试将所有物品替换为

$('select').selectBox(/* options*/).close(function () {
    alert($(this).val());
});

请注意,该插件似乎没有维护很长时间