【问题标题】:How to reset a dropdown list in jquery so nothing is selected?如何在 jquery 中重置下拉列表以便没有选择任何内容?
【发布时间】:2013-10-29 13:04:24
【问题描述】:

我在模态中有这个下拉列表

<div class="control-group">
    <label class="control-label" for="selectInsert01">Country Name:</label>
    <div class="controls">
        <select id="selectInsert01" class="input-xlarge with-search" data-bind="foreach: citiesModel.countriesList">
            <option data-bind="text: Name, value: CountryID"></option>
        </select>
    </div>
</div>

每次我选择某些内容并退出模式并再次打开它时,下拉列表中的选定值仍处于选中状态。如何重置值,以便退出模式时不选择任何内容

【问题讨论】:

  • 向我们展示您的模态脚本代码。

标签: jquery


【解决方案1】:

模式关闭

$("#selectInsert01 option:eq(0)").prop("selected", true); //set option of index 0 to selected

【讨论】:

    【解决方案2】:

    这对我有用。希望对您有所帮助。

    $("#selectInsert01 option:selected").removeAttr("selected");
    

    【讨论】:

      【解决方案3】:

      如果您在下拉列表中使用了引导类选择选择器:

      $('.selectpicker').selectpicker('val', '-1');
      

      您也可以参考这个链接:http://www.jquerybyexample.net/2012/03/how-to-reset-dropdown-using-jquery.html

      【讨论】:

        【解决方案4】:

        您可以将下拉菜单重置为

        $("#selectInsert01 option:first").attr("selected", true);
        

        但它只会更改下拉列表的选定值,而不是用户可见的标签,因此您也必须更改它。为此,您可以这样做:

        $(".control-label").text($("#selectInsert01 option:first").val()); 
        

        【讨论】:

          【解决方案5】:

          如果你有一个占位符,你可以这样做:

          $("#selectBox").val($("#selectBox option:first").val());
          

          如果你的占位符不包含值,甚至更简单:

          $("#selectBox").val(0);
          

          【讨论】:

            猜你喜欢
            • 2016-01-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多