jquery“.append”在选择框中出现问题

时间:2013-07-30 09:55:28

标签: javascript jquery append

我在html中有一个选择选项框。

<select name="selectDay" id="selectDay" data-mini="true" style="float: left;"></select>

在我使用jQuery .append方法附加一些选项后,这些选项不会出现。

function showbroadcastsDropDown() {
for (i = 0; i < dateArray.length; i ++ ) {

    // variables
    var month = dateArray[i].getMonth() + 1;
    var year = dateArray[i].getYear();
    year += 1900;

    //dateArray[i].getDate() + "-" + month + "-" + year

    if (i == 0) {
        console.log("0");
        $("#sendungen select[name='selectDay']").append('<option value="today"' + (actuallyDate == dateArray[i].getDate() ? ' selected=true' : '') + '>today</option>');        
    } else if (i == 1) {
        console.log("1");
        $("#sendungen select[name='selectDay']").append('<option value="tomorrow">tomorrow</option>');          
    } else {
        console.log("else");
        $("#sendungen select[name='selectDay']").append('<option value="">asdasdasd</option>');     
    }

    console.log(dateArray[i].getDate() + "-" + month + "-" + year);
}
$("#sendungen select[name='selectDay']").selectmenu("refresh"); 

不要询问dateArray var。

提前致谢!

4 个答案:

答案 0 :(得分:0)

尝试:

 $("#selectDay").
         .append($("<option></option>")
         .attr("value",key)
         .text(value)); 

答案 1 :(得分:0)

$("#sendungen select[name='selectDay']").selectmenu("refresh", true); // force rebuild

答案 2 :(得分:0)

尝试以下代码:

<select name="selectDay" id="selectDay" data-mini="true" style="float: left;"></select>
<input type="button" id="btnClick" />

Jquery的

$(document).ready(function(){    
$("#btnClick").click(function(){
$("#selectDay").append(new Option("option text", "value"));
});
});

答案 3 :(得分:0)

最后:)

for (i = 0; i < dateArray.length; i ++ ) {
    var month = dateArray[i].getMonth() + 1;
    if (month == 13) month = 1;
    var year = dateArray[i].getYear();
    year += 1900;

    if (i == 0) {
        $("#" + site + " select[name='selectDay']").append("<option value=\"" + dateArray[i].getDate() + "-" + month + "-" + year + "\" " + (actuallyDate == dateArray[i] ? "selected='true'" : "") + ">" + showbroadcastsDropDown_localize.today + "</option>");
    } else if (i == 1) {
        $("#" + site + " select[name='selectDay']").append("<option value=\"" + dateArray[i].getDate() + "-" + month + "-" + year + "\" " + (actuallyDate == dateArray[i] ? "selected='true'" : "") + ">" + showbroadcastsDropDown_localize.tomorrow + "</option>");        
    } else {
        $("#" + site + " select[name='selectDay']").append("<option value=\"" + dateArray[i].getDate() + "-" + month + "-" + year + "\" " + (actuallyDate == dateArray[i] ? "selected='true'" : "") + ">" + dateArray[i].getDate() + "-" + month + "</option>");        
    }
}

$("#" + site + " select[name='selectDay']").selectmenu("refresh");