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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:16:29  来源:igfitidea点击:

how to "reset" a combobox using jquery

jqueryselectreset

提问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

You could use the .val()function. And because the value of the element you want to preselect is empty you pass an empty string:

您可以使用该.val()功能。并且由于您要预选的元素的值为空,因此您传递了一个空字符串:

$('#color').val('');

回答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();