2012-04-27 136 views
0

我想用數組的值初始化一個選擇菜單,我想選擇一個特定的條目。我可以我想與此代碼值創建列表:JQuery/JQuery Mobile:更新選擇菜單

var myArray = new Array() 
myArray = ["a", "b", "b", "d", "e", "f"]; 


function updateSelectMenu(){ 
    for (var i = 0, len = myArray.length; i < len; i++) { 
     if (i == 2) { 
      $("#myArraySelectMenu") 
       .append($('<option>') 
       .attr('value', myArray[i]) 
       .attr('selected', "selected") 
       .text(myArray[i]+"text")) 
     } 
     else { 
      $("#myArraySelectMenu") 
       .append($('<option>') 
       .attr('value', myArray[i]) 
       .text(myArray[i]+"text")) 
     } 
    } 
} 

我的問題是這樣的:所選擇的條目不點擊查看所有選項前的下拉區域內出現。

Even if it is selected I cannot see the value at the top

Even if it is selected I cannot see the value at the top

謝謝你的幫助。

+0

你試試這樣的事情。 '.addClass(「yourClass」);''和'.removeClass(「yourClass」)' – 2012-04-27 12:10:46

+0

在jQuery手機中,我相信當你完成向標記中添加元素時,你需要調用'parentElement.trigger(「create」); '你會在你添加完所有元素後執行它。讓我知道,如果這有助於你 – veeTrain 2012-04-27 12:15:15

回答

0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>jQuery Mobile Docs - Select</title> 
    <link rel="stylesheet" href="../../../jquery.mobile-1.0.1.min.css" /> 
    <link rel="stylesheet" href="../../_assets/css/jqm-docs.css" /> 
    <script src="../../../jquery.js"></script> 
    <script src="../../../experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script> 
    <script src="../../_assets/js/jqm-docs.js"></script> 
    <script src="../../../jquery.mobile-1.0.1.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $.each(['a', 'b', 'c', 'd', 'e', 'f'], function (i, value) { 
       if (i == 2) { 
        $("#myArraySelectMenu").append($('<option />', { 'value': value, 'selected': 'selected' }).text(value + 'text')); 
       } 
       else { 
        $("#myArraySelectMenu").append($('<option />', { 'value': value }).text(value + 'text')); 
       } 
      }); 

      $("#myArraySelectMenu").selectmenu('refresh'); 
     }); 
    </script> 
</head> 
<body> 
    <div data-role="fieldcontain"> 
     <label for="select-choice-7" class="select"> 
      Array Mio:</label> 
     <select name="select-choice-7" id="myArraySelectMenu"> 
     </select> 
    </div> 
</body> 
</html> 
相關問題