无法为下拉列表设置默认值

Unable to set the default value for drop down list?

本文关键字:设置 默认值 下拉列表      更新时间:2023-09-26

在我的应用程序中有一个下拉列表,其中的值动态地来自数据库。如果我在下拉列表中选择任何类别值,然后选择菜单,如果我重置数据库,我需要清除下拉的选择值,并设置默认值"选择类别"。但是我无法清除下拉列表中的选定值。

我的代码如下,

脚本:

<script>
    function resetconformation()
    {
        var x;
        var r=confirm("Do you want to reset database!");
        if (r==true)
        {
        var db = window.sqlitePlugin.openDatabase({name: "MYDB"});
         db.transaction(function (tx) {
                           tx.executeSql('DROP TABLE IF EXISTS Locationlog');
                           });
        //$('#select-choice option:contains("Select Category")').prop('selected',true);
        $("#select-choice").empty();
        $("#locationList").empty();
        $("#maillocation").hide();
        var optionheading = '<option value="Select Category">Select Category</option>';
        $("#select-choice").append(optionheading);
        $("#select-choice").refresh();
        $('#select-choice option:contains("Select Category")').prop('selected',true);
        $('#select-choice option[value="Select Category"]').attr('selected',true);
        }
        else
        {
        }
    }
</script>
HTML:

<select name="select-choice" id="select-choice" data-mini="true" data-theme="a" style="width:230px">

选择类别

重置数据库后如何设置默认值"Select Category"?任何建议. .

示例:http://jsfiddle.net/Gajotres/tN56D/1/

你少了一个bug行:

$("#select-choice").selectmenu('refresh', true);

每一个动态添加的内容必须手动增强,jQuery Mobile不会为你做。

为什么不直接

var sel = $("#select-choice").get(0); // get the DOM object
sel.options.length=1; // no need to delete all and add the Select Category each time
sel.selectedIndex=0;