在select中设置selectedIndex

时间:2016-01-21 13:29:50

标签: jquery select

我想更改select元素的selectedIndex。如下所示,单击下拉字段时会标记selectedIndex。但为什么它不会在选择之上改变?在示例中,它仍然具有第一个选项(Test1)的名称。

编辑:我正在使用JQuery Mobile。

HTML:

SQLHANDLE hEnv;
SQLHANDLE hConnection;
LPCBYTE data = (LPCBYTE)malloc(100);

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hConnection);
SQLSetConnectAttr(hConnection, SQL_COPT_SS_BCP, (void *)SQL_BCP_ON, SQL_IS_INTEGER) ;
SQLConnect(hConnection, "DSN", SQL_NTSL, NULL, 0, NULL, 0);
bcp_init(hConnection, "DB_TABLE", NULL, NULL, DB_IN);
bcp_bind(hConnection, data, 0, SQL_VARLEN_DATA, (LPCBYTE)"", 1, SQLCHARACTER, 1);

使用Javascript:

 <div class="ui-field-contain">
                               <select name="select-native-3"id="selectRule"
                            data-iconpos="left">
                            <option value="0">Test 1</option>
                            <option value="1">Test 2</option>
                            <option value="2">Test 3></option>     
                        </select> </div>

结果:

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试:

$(function() {
    $("#selectRule").val(2);
});

答案 1 :(得分:0)

    <html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>

    $(document).ready(function () {

        function JXX_SelectOption(Index)
        {
            $('#selectRule option:eq(' + Index + ')').attr("selected", "selected");
        }
        JXX_SelectOption(1);

    });
</script>
</head>
<body>
    <div class="ui-field-contain">
                               <select name="select-native-3"id="selectRule"
                            data-iconpos="left">
                            <option value="0">Test 1</option>
                            <option value="1">Test 2</option>
                            <option value="2">Test 3</option>     
                        </select> </div>

</body>
</html>