[英]How to reset multiple select boxes in a form with jquery?
How can I reset multiple select boxes in a form with jquery?如何使用 jquery 在表单中重置多个选择框?
I have this so far, it resets everything but the selects.到目前为止,我有这个,它重置了除选择之外的所有内容。
$('button#reset').click(function(){
$form = $('button#reset').closest('form');
$form.find('input:text, input:password, input:file, select, textarea').val('');
$form.find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
$form.find('select').selectedIndex = 0;
});
Added some markup:添加了一些标记:
<form id="form" name="form" method="post" class="form" role="form" action="inventory-search" enctype="multipart/form-data">
<div class="form-group">
<label for="grade">Grade</label>
<select name="grade" class="form-control input-sm" onchange="this.form.submit();">
<option value="">Any</option>
<option value="opt1">opt1</option>
<option value="opt2" selected="selected">opt2</option>
</select>
</div>
<!-- there are 6 more select controls -->
<div class="form-group">
<label> </label>
<button type="submit" name="search" id="search" value="search" class="btn button-sm btn-primary">Search</button>
</div>
<div class="form-group">
<label> </label>
<button type="reset" name="reset" id="reset" value="reset" class="btn button-sm btn-primary">Reset</button>
</div>
</form>
这将起作用:-
$form.find('select').prop('selectedIndex',0);
我使用此代码成功取消选择表单中的所有 html 选择:
$("#form1").find('select').prop('selectedIndex', -1);
Change the last line to this:将最后一行更改为:
$form.find('select')[0].selectedIndex = 0;
selectedIndex
is a property of HTMLElement
and not jQuery object. selectedIndex
是HTMLElement
的属性,而不是 jQuery 对象。
If you know the value, then you can do $form.find('select').val('value');
如果您知道该值,那么您可以执行
$form.find('select').val('value');
Below code worked for me:下面的代码对我有用:
$('.formID').find('select[id="selectID1"]').removeAttr('selected').trigger('change');
$('.formID').find('select[id="selectID2"]').removeAttr('selected').trigger('change');
$form.find('select').each(function(){
$(this).val($(this).find('option:first').val());
});
This one works for me...这个对我有用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.