我有几个下拉框,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);
});
});
});
这行不通。更改所选国家/地区时没有语法错误,但没有任何反应。我确信每个循环都执行了几次,并且数组包含选择框。
对此有任何想法吗?
谢谢,保罗