【发布时间】:2017-12-18 20:52:02
【问题描述】:
我正在尝试动态创建一个select 元素,但它不是由 jQuery Mobile 设计的。实现这一目标的正确方法是什么?
【问题讨论】:
标签: javascript jquery html css jquery-mobile
我正在尝试动态创建一个select 元素,但它不是由 jQuery Mobile 设计的。实现这一目标的正确方法是什么?
【问题讨论】:
标签: javascript jquery html css jquery-mobile
这是一个有效的 jsFiddle 示例:http://jsfiddle.net/Gajotres/dEXac/
$(document).on('pagebeforeshow', '#index', function(){
// Add a new select element
$('<select>').attr({'name':'select-choice-1','id':'select-choice-1'}).appendTo('[data-role="content"]');
$('<option>').attr({'value':'1'}).html('Value 1').appendTo('#select-choice-1');
$('<option>').attr({'value':'2'}).html('Value 2').appendTo('#select-choice-1');
// Enhance new select element
$('select').selectmenu();
});
【讨论】:
值得注意的是,这也应该有效:(示例附加两个选项以显示如何添加选项。
$('<select name="sl" id="sl" data-native-menu="false"><option value="clear">That I got</option></select>').appendTo("#output");
$('<option value="clear2">Another That I got</option>').appendTo("#sl");
$('select').selectmenu();
【讨论】:
这个对我有用
$(document).on('change', "body", function(){
$( ".ui-selectmenu" ).selectmenu();
});
【讨论】: