jQuery 1.4.2对doropdown列表的更改事件使用.delegate() [英] jQuery 1.4.2 Using .delegate() on a change event for a doropdown list
问题描述
我的问题是我不确定如何使用.delegate来实现以下场景:
My problem is that I am not sure how to use .delegate for the following scenario:
我们的应用程序有一个投票系统可以进行多轮或步骤添加。每次添加新步骤时,都会有一个选项列表,用于定义如何赢得回合/步骤。
our application has a voting system to which several rounds or steps can be added. every time a new step is added there is a list of options that defines how the round/step is to be won.
<select class="listOfOptions">
<option value="U">Unanimous</option>
<option value="M">Majority</option>
<option value="C" class="customOption"># of votes…</option>
</select>
现在当选择一个选项时,以下代码运行
now when an option is selected the following code runs
$(document).ready(function() {
$('.listOfOptions').live('change', function() {
if ($(this).find(':selected').attr('class') == 'customOption') {
// DO SOMETHING!!
}
else {
// DO SOMETHING ELSE
}
});
});
此代码在IE以外的所有其他浏览器上运行完美。
This code runs perfectly on every other browser except IE.
在这种情况下,我如何使用.delegate()代替.live?
谢谢。
推荐答案
对于这种特殊情况,这将是与.delegate()一起使用的语法。请注意,'listOfOptions'是下拉列表的类。
This would be the syntax used with .delegate() for this particular case. Notice that the 'listOfOptions' is the class of the drop-down list.
$('body').delegate('.listOfOptions', 'change', function() {
if ($(this).find(':selected').attr('class') == 'customOption') {
// DO SOMETHING!!
}
else {
// DO SOMETHING ELSE
}
});
它在所有浏览器中都像魅力一样。
It works like a charm in all browsers.
这篇关于jQuery 1.4.2对doropdown列表的更改事件使用.delegate()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!