jquery创建的下拉列表未显示在ie8中

时间:2011-09-02 18:35:00

标签: javascript jquery drop-down-menu

我有创建下拉列表的代码,但在IE8中,下拉列表具有正确的条目数,但文本为空。它们确实包含正确的值。我看不出有什么遗漏?

if(max_ch>0){
    var newDiv = $('<div>Room '+(i+1)+' <select class="adu" name="data[Rate]['+r_id+']['+ro_id+'][adults][]"></select> adults. <select class="chi" name="data[Rate]['+r_id+']['+ro_id+'][children][]"></select> children.</div>');
    newDiv.attr("id","occupants"+i).appendTo(showdiv+' .rooms_adults');

    var roomPrice = $('<input type="hidden" name="data[Rate]['+r_id+']['+ro_id+'][prices][]" value="'+room_bo+'" />');
    roomPrice.attr("id","roomprice"+i).appendTo(showdiv+' .rooms_adults');

    var num_opts = Number(max_ad)+1;
    for( ad=0; ad < num_opts; ad++){
        $(showdiv+' #occupants'+i+' select.adu').append(new Option(ad, ad));
    }
    var num_opts = Number(max_ch)+1;
    for( ch=0; ch < num_opts; ch++){
        $(showdiv+' #occupants'+i+' select.chi').append(new Option(ch, ch));                    
    }
    $(showdiv+' #occupants'+i+' select.adu').val('1');

} else {

2 个答案:

答案 0 :(得分:2)

您可以使用

var num_opts = Number(max_ad) + 1,
    slc_adu = $(showdiv+' #occupants'+i+' select.adu');
for( ad=0; ad < num_opts; ad++){
    slc_adu.append("<option value=\"" + ad + "\">" + ad + "</option>");
}

var num_opts = Number(max_ad) + 1,
    slc_adu = $(showdiv+' #occupants'+i+' select.adu'),
    options = slc_adu.attr("option");
for( ad=0; ad < num_opts; ad++){
    options[options.length] = new Option(ad, ad);
}

否则IE不会显示选项的关联文本值。

答案 1 :(得分:1)

Josh的回答对我有用:Adding options to a <select> using jQuery?

var o = new Option("option text", "value");
/// jquerify the DOM object 'o' so we can use the html method
$(o).html("option text");
$("#selectList").append(o);