如何使用 jquery 重置表单中的多个选择框?
- 有多个选择框
- 它们是动态生成的,我们不知道它们会是什么
- 一些框选项标签将被标记为选中
- fiddle :http://jsfiddle.net/Qnq82/
到目前为止我有这个,它会重置除选择之外的所有内容。
$('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;
});
添加了一些标记:
<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);
关于javascript - 如何使用 jquery 重置表单中的多个选择框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24771308/