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

后端 未结 10 804
情话喂你
情话喂你 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:35

    This is helpfull, its work for me..

    $('.bd-example-modal-sm').on('hidden.bs.modal', function () { 
          $(this).find("select").val('').end(); // Clear dropdown content
          $("ul").empty();   // Clear li content 
    });
    
    0 讨论(0)
  • 2020-12-04 11:35

    Put the contents in your modal inside a form and give it an ID which is equal to "myForm".

    When the close button is clicked, give an onclick to the function "myFunction()".

    <button class="btn btn-default" data-dismiss="modal" onclick="myFunction()">Cancel</button>
    
    function myFunction() {
                document.getElementById("myForm").reset();
            }
    
    0 讨论(0)
  • 2020-12-04 11:35

    In addition to @Malk, I wanted to clear all fields in the popup, except the hidden fields. To do that just use this:

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

    This will clear all fields, except the hidden ones.

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

    The following worked for me:

    $(':input').val('');
    

    However, it is submitting the form, so it might not be what you are looking for.

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