【问题标题】:Unable to set the default value for drop down list?无法设置下拉列表的默认值?
【发布时间】:2014-07-28 11:07:03
【问题描述】:

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

我的代码如下,

脚本:

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

选择类别

重置数据库后如何设置默认值“选择类别”?任何建议..

【问题讨论】:

  • 好像是working fine
  • 但请缓存对象或链接语句
  • 是的,但不在我的应用程序中。我正在使用侧面板作为菜单列表。重置数据库是其中的一种选择。单击重置数据库时,它将显示一个警告框。单击是,我正在重置数据库。
  • 如何缓存对象或链接语句?
  • 除非您添加了自定义 jquery 函数。 $("#select-choice").refresh(); 会抛出错误.. 其他一切似乎都很好。

标签: javascript jquery html sqlite jquery-mobile


【解决方案1】:

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

您缺少一个错误行:

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

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

【讨论】:

    【解决方案2】:

    为什么不直接

    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;
    

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 2020-07-06
      • 2016-08-23
      • 2016-11-16
      • 2016-09-16
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多