jQuery 如何使用jquery“重置”组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7779658/
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 combobox using jquery
提问by leora
i have a select dropdown on a webpage:
我在网页上有一个选择下拉列表:
<select class="designs" id="color" name="Color">
<option value="">Select Color . . .</option>
<option value="Dark">Dark</option>
<option value="White">White</option>
<option value="Light">Light</option>
</select>
and i am trying to find the right jquery code to select the first entry on that combobox (the one with option value ="" and the text of "Select Color . . ." (this is the setting that is on the page during original load before the user makes any changes)
我正在尝试找到正确的 jquery 代码来选择该组合框上的第一个条目(选项值为 ="" 和“选择颜色...”的文本(这是原始页面上的设置)在用户进行任何更改之前加载)
I am doing this off of a button click link that says "Reset".
我正在通过一个显示“重置”的按钮单击链接来执行此操作。
回答by Darin Dimitrov
回答by Jakub Konecki
$('#color option:first-child').attr("selected", "selected");
$('#color')[0].selectedIndex = 0;
回答by Its Aafrin
You can use the .empty() function to clear the option of the combo box.
您可以使用 .empty() 函数清除组合框的选项。
$('#color').empty();