Form inside Boostrap Modal does not reset

北城以北 提交于 2020-01-06 06:29:31

问题


I am using below click event to reset Form.

'click .resetBulkAssignForm' : function(events, template){
    console.log('Reset', $(".bulkAssignForm")[0]);
    $(".bulkAssignForm")[0].reset();
    $(".bulkAssignForm").find("select").val("");
    $('#firmName').select2('data', null);
},

I also tried most of the answers available on SO to solve this issue.

Question: How to reset form inside Bootstrap Modal?

Below is the image of the modal with console output.

NOTE: I am using AdminLTE 2.3.11, Select2. Also kindly take a note that I have already tried solutions in SO links like how-to-clear-all-input-fields-in-bootstrap-modal-when-clicking-data-dismiss-butt, how-to-reset-form-body-in-bootstrap-modal-box

Adding HTML Code in case you need to see.

<div id="myBulkModal" class="modal fade modal-primary" role="dialog">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h2 class="modal-title">Bulk Assignment</h2>
        </div>
        <div class="modal-body">
           <section class="content">
             <form class="bulkAssignForm">
               <fieldset>
                 <div class="row">
                    <input type="hidden" id="taskIdInput" value="{{getTaskId}}" />
                 </div>
                 <div class="row">
                   <div class="input-group col-sm-12">
                     <label for="firmName">Firms</label><br />
                     <select id="firmName" multiple class="form-control input-lg" required>
                       {{#each firmNamesFromAssignment}}
                          <option value="{{value}}">{{label}}</option>
                       {{/each}}
                     </select>
                   </div>
                 </div>
                 <br />
                 <div class="row">
                   <div class="input-group col-sm-12">
                     <label for="assignee">Assignee</label><br />
                     <select id="assignee" class="form-control input-lg" required>
                       <option  selected="selected" value="">Select Option</option>
                       {{#each usersSelect2}}
                          <option value="{{value}}">{{label}}</option>
                       {{/each}}
                     </select>
                   </div>
                 </div>
                 <br />
                 <div class="row">
                   <div class="input-group col-sm-12">
                     <label for="reviewedBy">Reviewer</label><br />
                     <select id="reviewedBy" class="form-control input-lg" required>
                       <option  selected="selected" value="">Select Option</option>
                       {{#each usersSelect2}}
                          <option value="{{value}}">{{label}}</option>
                       {{/each}}
                     </select>
                   </div>
                 </div>
                 <br />
                 <div class="row">
                   <!-- buttons -->
                   <button type="submit" class="btn btn-outline">
                     <span class="glyphicon glyphicon-ok"></span> Assign
                   </button>
                   &nbsp;&nbsp;
                   <button type="button" class="btn btn-outline resetBulkAssignForm">
                     <span class="glyphicon glyphicon-off"></span> Reset
                   </button>
                   &nbsp;&nbsp;
                   <button type="button" class="btn btn-outline" data-dismiss="modal">
                     <span class="glyphicon glyphicon-refresh"></span> Close
                   </button>

                 </div>
               </fieldset>
             </form>
           </section>
        </div>
      </div>
    </div>
  </div>

回答1:


After changing value of Select2's underlying <select> or <input> you have to trigger change event so this change will be handled by Select2 code and rendered appropriately:

$(".bulkAssignForm").find("select").val("").trigger("change");

Successfully tested on AutoForm Demo test page.



来源:https://stackoverflow.com/questions/46449665/form-inside-boostrap-modal-does-not-reset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!