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

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

提交回复
热议问题