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

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

提交回复
热议问题