How to clear all input fields in bootstrap modal when clicking data-dismiss button?

后端 未结 10 803
情话喂你
情话喂你 2020-12-04 10:58

How to clear all input fields in a Bootstrap V3 modal when clicking the data-dismiss button?

相关标签:
10条回答
  • 2020-12-04 11:09

    I did it in the following way.

    1. Give your form element (which is placed inside the modal) anID.
    2. Assign your data-dimiss an ID.
    3. Call the onclick method when data-dimiss is being clicked.
    4. Use the trigger() function on the form element. I am adding the code example with it.

       $(document).ready(function()
           {
          $('#mod_cls').on('click', function () {
        $('#Q_A').trigger("reset");
          console.log($('#Q_A'));
       })
        });
      

        <div class="modal fade " id="myModal2" role="dialog" >
        <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" ID="mod_cls" data-dismiss="modal">&times;</button>
          <h4 class="modal-title" >Ask a Question</h4>
        </div>
        <div class="modal-body">
          <form role="form" action="" id="Q_A" method="POST">
            <div class="form-group">
              <label for="Question"></label>
              <input type="text" class="form-control" id="question" name="question">
            </div>
    
          <div class="form-group">
              <label for="sub_name">Subject*</label>
              <input type="text" class="form-control" id="sub_name" NAME="sub_name">
            </div>
            <div class="form-group">
              <label for="chapter_name">Chapter*</label>
              <input type="text" class="form-control" id="chapter_name" NAME="chapter_name">
            </div>
            <button type="submit" class="btn btn-default btn-success btn-block"> Post</button>
                                   </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button><!--initially the visibility of "upload another note" is hidden ,but it becomes visible as soon as one note is uploaded-->
          </div>
          </div>
          </div>
          </div>
    

    Hope this will help others as I was struggling with it since a long time.

    0 讨论(0)
  • 2020-12-04 11:12

    If you are using a form in the modal then you can use

    $("#form_id").trigger("reset");
    
    0 讨论(0)
  • 2020-12-04 11:24

    There is a more easy and beautiful way:

    $('#MyModal').on('hidden.bs.modal', function () {
        $(this).find('form').trigger('reset');
    })
    

    reset is dom build-in funtion, you can also use $(this).find('form')[0].reset();

    And Bootstrap's modal class exposes a few events for hooking into modal functionality, detail at here.

    hide.bs.modal This event is fired immediately when the hide instance method has been called.

    hidden.bs.modal This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).

    0 讨论(0)
  • 2020-12-04 11:28

    http://getbootstrap.com/javascript/#modals shows an event for when a modal is hidden. Just tap into that:

    $('#modal1').on('hidden.bs.modal', function (e) {
      $(this)
        .find("input,textarea,select")
           .val('')
           .end()
        .find("input[type=checkbox], input[type=radio]")
           .prop("checked", "")
           .end();
    })
    

    http://jsfiddle.net/5LCSU/


    I would suggest the above as it bind the clearing to the modal itself instead of the close button, but I realize this does not address your specific question. You could use the same clearing logic bound to the dismiss buttons:

    $('[data-dismiss=modal]').on('click', function (e) {
        var $t = $(this),
            target = $t[0].href || $t.data("target") || $t.parents('.modal') || [];
    
      $(target)
        .find("input,textarea,select")
           .val('')
           .end()
        .find("input[type=checkbox], input[type=radio]")
           .prop("checked", "")
           .end();
    })
    

    http://jsfiddle.net/jFyH2/

    0 讨论(0)
  • 2020-12-04 11:32

    enclose your modal body inside a form with an id="myform"

    and then

     $("#activatesimModal").on("hidden.bs.modal",function(){
            myform.reset();
    });
    

    should do the trick

    0 讨论(0)
  • 2020-12-04 11:34
    $('[data-dismiss=modal]').on('click', function (e) 
    
    {
    
    var $t = $(this),
    
            target = $t[0].href || $t.data("target") || $t.parents('#myModal') || [];
    
       $(target)
    
        .find("input")
           .val('')
           .end()
        .find("input[type=checkbox]")
           .prop("checked", " ")
           .end();
    
    
    
    $("span.inerror").html(' ');
    
    $("span.inerror").removeClass("inerror");
    
    document.getElementById("errorDiv1").innerHTML=" ";
    
    })      
    

    This code can be used on close(data-dismiss)of modal.(to clear all fields)

    1. Here I have cleared my input fields and my div as id="errorDiv1" which holds all validation errors.

    2. With this code I can also clear other validation errors having class as inerror which is specified in span tag with class inerror and which was not possible using document.getElementsByClassName

    0 讨论(0)
提交回复
热议问题