2011-12-21 169 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