如何在 jquery 中重置下拉列表以便没有选择任何内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19659038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reset a dropdown list in jquery so nothing is selected?
提问by Marko Kosanovic
I have this drop down list in a modal
我在模态中有这个下拉列表
<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>
Every time I select something and exit the modal and than open it again the selected value in the dropdown list is still selected. How do I reset the values so nothing is selected when I exit the modal
每次我选择一些东西并退出模式然后再次打开它时,下拉列表中的选定值仍然被选中。如何重置值以便退出模态时不选择任何内容
回答by tymeJV
On modal close
在模态关闭时
$("#selectInsert01 option:eq(0)").prop("selected", true); //set option of index 0 to selected
回答by You Rule Avi
This worked for me. Hope it helps.
这对我有用。希望能帮助到你。
$("#selectInsert01 option:selected").removeAttr("selected");
回答by Renu
You can reset your dropdown as
您可以将下拉列表重置为
$("#selectInsert01 option:first").attr("selected", true);
But it will only change the selected value of dropdown but not the label that is visible to user so you have to change that also. For that you can do :
但它只会更改下拉列表的选定值,而不会更改用户可见的标签,因此您也必须更改它。为此,您可以这样做:
$(".control-label").text($("#selectInsert01 option:first").val());
回答by Prabhjyot Saini
If you have used bootstrap class selectpicker in your drop-down list:
如果您在下拉列表中使用了引导类 selectpicker:
$('.selectpicker').selectpicker('val', '-1');
You can also refer this link: http://www.jquerybyexample.net/2012/03/how-to-reset-dropdown-using-jquery.html
你也可以参考这个链接:http: //www.jquerybyexample.net/2012/03/how-to-reset-dropdown-using-jquery.html
回答by supertramp
If you have a placeholder you can do:
如果您有占位符,您可以执行以下操作:
$("#selectBox").val($("#selectBox option:first").val());
Or even simpler if your placeholder does not hold a value:
如果您的占位符没有值,甚至更简单:
$("#selectBox").val(0);