jquery - 使用 jquery mobile 填充下拉列表

标签 jquery html jquery-mobile

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

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

上面的 fiddle 有我目前正在使用的代码。

最佳答案

您面临的问题是您需要“刷新”<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/

关于jquery - 使用 jquery mobile 填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8595367/

相关文章:

javascript - 什么是正确的 Javascript 绑定(bind)语法?

jquery - 在加载时折叠自定义 jquery Accordion

javascript - 在 ajax 请求期间从查询字符串 url 中删除 'X-Requested-With'

jquery - 如何删除 float ,使元素不只在这个 Codepen 中用 CSS 包装

javascript - 如何在 jQuery Mobile 中顺序显示多个对话框?

javascript - 异步函数无法使用 JQuery 工作

html - HTML/CSS 菜单的小错误

html - 在按钮文本上方显示 Font Awesome 图标

php - JQuery 移动登录页面

css - 多个 jQuery Mobile 计数气泡