简体   繁体   English

如何在jQuery Mobile中以编程方式选择下拉列表的选项

[英]how to programmatically select an option of a dropdown list in jquery mobile

I need to select an option from the dropdownlist programmatically as soon as the document gets ready 文档准备好后,我需要以编程方式从下拉列表中选择一个选项

Like for example i have this code 例如,我有此代码

<div class="ui-select">
            <select name="DropDownList" id="DropDownList"
                data-native-menu="false" tabindex="-1">
                <option value="data1">Data1</option>
                <option value="data2">Data2</option>
                <option value="data3">Data3</option>
                <option value="data4">Data4</option>
            </select>
        </div>

Now if i select it manually then the option gets selected.But i need to select an option pro-grammatically as soon as document gets ready.How to do it? 现在,如果我手动选择它,那么该选项将被选择。但是我需要在文档准备好后尽快在语法上选择一个选项。

var myselect = $("select#foo");
myselect[0].selectedIndex = 3;
myselect.selectmenu("refresh");

Script taken from: http://jquerymobile.com/demos/1.0a4/docs/forms/forms-selects.html 脚本取自: http : //jquerymobile.com/demos/1.0a4/docs/forms/forms-selects.html

You can get the selected option using jQuery and use jQUery mobile event 'pageshow' which is the equivalent to 'ready' event on jQuery but not advised to mobile sites 您可以使用jQuery获得选定的选项,并使用jQUery移动事件“ pageshow”,它等效于jQuery上的“就绪”事件,但不建议移动网站使用

$("div[data-role*='page']").live('pageshow', function(event, ui) {
    $("#DropDownList").find(":selected").val();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM