如何使用 jQuery 清除按钮单击事件上的下拉列表值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2985072/
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 do I clear the dropdownlist values on button click event using jQuery?
提问by kumar
How do I clear the dropdownlist values on button click event using jQuery?
如何使用 jQuery 清除按钮单击事件上的下拉列表值?
回答by jAndy
$('#dropdownid').empty();
That will remove all <option>
elements underneath the dropdown element.
这将删除<option>
下拉元素下方的所有元素。
If you want to unselect selected items, go with the code from Russ.
如果要取消选择所选项目,请使用 Russ 的代码。
回答by user113716
A shorter alternative to the first solution given by Russ Camwould be:
Russ Cam给出的第一个解决方案的一个更短的替代方案是:
$('#mySelect').val('');
This assumes you want to retain the list, but make it so that no option is selected.
这假设您要保留列表,但不要选择任何选项。
If you wish to select a particular default value, just pass that value instead of an empty string.
如果您希望选择特定的默认值,只需传递该值而不是空字符串。
$('#mySelect').val('someDefaultValue');
or to do it by the index of the option, you could do:
或者通过选项的索引来做,你可以这样做:
$('#mySelect option:eq(0)').attr('selected','selected'); // Select first option
回答by Russ Cam
If you want to reset the selected options
如果要重置所选选项
$('select option:selected').removeAttr('selected');
If you actually want to removethe options (although I don't think you mean this).
如果你真的想删除这些选项(虽然我不认为你是这个意思)。
$('select').empty();
Substitute select
for the most appropriate selector in your case (this may be by id or by CSS class). Using as is will reset all <select>
elements on the page
select
在您的情况下替换最合适的选择器(这可能是通过 id 或 CSS 类)。按原样使用将重置<select>
页面上的所有元素
回答by Prabhjyot Saini
If you want to reset bootstrap page with button click using jQuery :
如果要使用 jQuery 通过单击按钮重置引导程序页面:
function resetForm(){
var validator = $( "#form_ID" ).validate();
validator.resetForm();
}
Using above code you also have change the field colour as red to normal.
使用上面的代码,您还可以将字段颜色更改为红色到正常。
If you want to reset only fielded value then :
如果您只想重置字段值,则:
$("#form_ID")[0].reset();