2011-12-21 166 views
3

我正在從數據庫填充下拉列表。我正在使用jQuery/jQuery的移動。我可以填充下拉菜單。我的問題是,當它被填充時,它不顯示第一個值,除非我選擇其他元素,然後再次選擇第一個元素。使用jquery mobile填充下拉列表

http://jsfiddle.net/hozefa/TtguK/

上面的小提琴有我目前使用的代碼。

+0

可能:http://jsfiddle.net/TtguK/11/ – 2011-12-21 20:06:50

回答

4

你所面臨的問題是,你需要 「刷新」 <select>部件使用.selectmenu('refresh')

var temp = ['5.00', '10.00', '15.00', '25.00', '50.00', '100.00'], 
    output = []; 

//notice I cached the `temp.length` value, the `for` loop will perform faster if this is done 
for(var i = 0, len = temp.length; i < len; i++){ 

    //instead of appending each `<option>` element, it is a better practice to either concoct a string of all the HTML or create an array that will later be turned into a string (here we are pushing new indexes onto an `output` array) 
    output.push('<option value="' + temp[i]+'">' + temp[i] + '</option>'); 
} 

//now make a single `.append()` call with all the HTML in one big string 
//and most importantly, call `.selectmenu("refresh")` after we update the HTML of the select menu so the jQuery Mobile framework will update the widget 
$('#amountsList').append(output.join('')).selectmenu('refresh'); 

這裏是一個演示:http://jsfiddle.net/TtguK/9/

+0

謝謝老兄! – Hozefa 2011-12-21 20:04:23

+1

@Hozefa不客氣。如果你只從這個信息中取出一個信息,記住每個jQuery Mobile小部件都有類似的東西,它們都可以通過編程方式創建/刷新。 – Jasper 2011-12-21 20:05:27