2015-05-19 119 views
0

我已经动态创建了选择菜单。我如何动态地将选定的值更改为第三个值(Apple)。动态更改jQuery Mobile选择框的选择值

HTML

<div> 
    <select id="stylex" data-mini="true"> </select> 
</div> 

代码

var wid_settings = ["Banana","Orange","Apple","Mango"]; 

wid_settings_refresh(); 

function wid_settings_refresh() { 
    var index;  
    for (index = 0; index < wid_settings.length; index++) { 
     $('#stylex').append('<option value='+index+'>'+wid_settings[index]+'</option>'); 
    } 
    $('#stylex').listview('refresh'); 
} 

// this is not working 
$('#stylex').val(3); 
$('#stylex').selectmenu("refresh"); 

JSFiddle

+0

来自JSFiddle的代码添加到问题中。 JSFiddle链接格式正确。 –

回答

0

更新您的JS提琴:http://jsfiddle.net/rhLt2sxj/5/

var wid_settings = ["Banana", "Orange", "Apple", "Mango"]; 

wid_settings_refresh(); 

function wid_settings_refresh() { 
    var index; 
    for (index = 0; index < wid_settings.length; index++) { 

     $('#stylex').append('<option value=' + index + '>' + wid_settings[index] + '</option>'); 
    } 
    $('#stylex').val(3); 
    //$('#stylex').listview('refresh'); //not necessary 
} 
+0

谢谢,它在“wid_settings_refresh”函数内部工作,但如果我想在单独的函数中更改值,该怎么办? jsfiddle.net/rhLt2sxj/6/ – user3189644

+0

http://jsfiddle.net/rhLt2sxj/7/ –

+0

基本上你的$('#stylex')。listview('refresh');是错的。并在那里执行正在停止 –