0

我正在尝试使用数组的值初始化一个选择菜单,并且我希望选择一个特定的条目。我可以使用以下代码创建包含所需值的列表:

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"))
        }
    }
}

我的问题是:在单击查看所有选项之前,所选条目未出现在下拉区域中。

即使它被选中,我也看不到顶部的值

即使它被选中,我也看不到顶部的值

感谢您的帮助。

4

1 回答 1

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>
于 2012-04-28T10:17:27.213 回答