简体   繁体   English

如何使用jquery重置表单中的多个选择框?

[英]How to reset multiple select boxes in a form with jquery?

How can I reset multiple select boxes in a form with jquery?如何使用 jquery 在表单中重置多个选择框?

  • there are multiple select boxes有多个选择框
  • they are dynamically generated & we don't know what they will be它们是动态生成的,我们不知道它们会是什么
  • some of the boxes option tags will be marked as selected一些框选项标签将被标记为选中
  • fiddle: http://jsfiddle.net/Qnq82/小提琴: http : //jsfiddle.net/Qnq82/

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>&nbsp;</label>
        <button type="submit" name="search" id="search" value="search" class="btn button-sm btn-primary">Search</button>
    </div>

    <div class="form-group">
        <label>&nbsp;</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);

I tried and searched a lot on StackOverFlow but didn't get any solution to reset multiselect.Then i found This Link .我在 StackOverFlow 上尝试并搜索了很多,但没有得到任何重置多选的解决方案。然后我找到了This Link That solved my Problem.那解决了我的问题。 Here is the code sample.这是代码示例。

$("#yourcontrolid").multiselect('clearSelection');

Change the last line to this:将最后一行更改为:

$form.find('select')[0].selectedIndex = 0;

selectedIndex is a property of HTMLElement and not jQuery object. selectedIndexHTMLElement的属性,而不是 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM