2013-07-30 35 views
0

我在html中有一個select選項框。在選擇框中的jquery「.append」問題

<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。

在此先感謝!

回答

0

finall Ÿ:)

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"); 
0

嘗試:

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

不要工作太... :( –

0

嘗試

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

不工作太 –

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")); 
}); 
}); 
+0

噓:)做得好:)它增加了一些選項:DD謝謝:) –

+0

請欣賞的答案 –

+0

檢查HTTP ://jsfiddle.net/nFdtJ/4/ –