使用jquery mobile填充下拉列表

时间:2011-12-21 19:41:51

标签: jquery html jquery-mobile

我正在努力填充数据库中的下拉列表。我正在使用jquery / jquery mobile。我能够填充下拉列表。我的问题是,当它被填充时,它不显示第一个值,除非我选择一些其他元素然后再次选择第一个。

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

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

1 个答案:

答案 0 :(得分: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/