jQuery 1.10.2 $ .each函数不起作用 [英] Jquery 1.10.2 $.each function not working
问题描述
好的,首先让我描述一下我的HTML.
Ok, first let me describe my HTML.
我有一个选择列表名称和ID,即upload_Gcat,它具有三个选项.然后,我有一个选择列表的名称和ID为upload_album.我想要的是随着upload_Gcat的更改而改变,upload_album根据选择的内容更改其选项.我已经查询到删除旧版本为止.但是当它到达我的$ .each函数时,它不起作用.
I have a select list name and ID as upload_Gcat that has three options. I then have a select list name and ID as upload_album. What I want is as upload_Gcat is changed upload_album changes its options according to what was selected. I have gotten my query to work as far as removing the old. But when it reaches my $.each function it does not work.
我在$ .each函数中尝试了各种不同的代码行,甚至尝试了错误的警报调试方式,在$ .each内部都未显示任何警报.
I have tried various different code lines inside the $.each function, even tried the bad debug way of alerts, no alert showed up while inside of $.each.
var new_options = new Array();
new_options["album1"] = "album1";
new_options["album2"] = "album2";
new_options["album3"] = "album3";
var select = $('upload_album');
if(select.prop){
var options = select.prop('options');
}
else{
var options = select.attr('options');
}
$('option', select).remove();
$.each(new_options, function(key, value) {
options[options.length] = new Option(value, value);
});
推荐答案
您可以尝试一下(使用object
代替array
,JavaScript不支持关联数组)
You may try this (Use an object
instead of array
, JavaScript doesn't support associative array)
var new_options = {}; // an empty object
new_options["album1"] = "album1";
new_options["album2"] = "album2";
new_options["album3"] = "album3";
var select = $('#upload_album').empty();
$.each(new_options, function(key, value) {
var newOption = $('<option/>', {'value':key, 'text':value});
select.append(newOption);
});