【发布时间】:2013-01-03 16:34:34
【问题描述】:
我有几个下拉框,ID 为 country1, country2, ... 当国家/地区在下拉列表中更改时,该国家/地区的值应显示在警报框中。
如果我像这样为一个盒子添加 onchange 处理程序,它可以正常工作:
$('#country1') .live('change', function(e){
var selectedCountry = e.target.options[e.target.selectedIndex].value;
alert(selectedCountry);
});
但我需要为所有下拉框动态执行此操作,所以我尝试了:
$(document).ready(function() {
$('[id^=country]') .each(function(key,element){
$(this).live('change', function(e){
var selectedCountry = e.target.options[e.target.selectedIndex].value;
alert(selectedCountry);
});
});
});
这不起作用。更改所选国家/地区时没有语法错误,但没有任何反应。我确信每个循环都执行了几次,并且数组包含选择框。
对此有什么想法吗?
谢谢, 保罗
【问题讨论】:
-
.live() 方法已被弃用。请改用 .on()。
-
@MaggiQall:ID 是唯一的 id^=country 意味着它以国家开头
标签: javascript jquery onchange