关闭模态后清除模态字段 [英] Clear modal fields after closing modal
问题描述
我有这个模态
<div class="modal-body">
我想要的是每次关闭时清除模态,所以我写了一个这样的脚本:
function clear() {$("#txtNombreCompleto").val("");$("#txtNombreEmpresa").val("");$("#exampleInputEmail2").val("");$("#dropOficina").val("");$("#txtTelefono").val("");$("#txtMensaje").val("");}$('#mymodal').on('隐藏', function(){$.clear(this)});
所以我在模态中的输入是这样的:
但是当我关闭模态时,它不运行该功能,我如何在模态关闭时运行该功能?问候
只需在 bootstrap modal hide.bs.modal
事件(或 hidden.bs.modal
) 处理程序或将函数设置为回调.还将您的代码放入 document ready handler
用于在加载页面后附加事件处理程序.
$(document).ready(function() {$('#mymodal').on('隐藏', function() {清除()});});
<小时>
或获取所有表单中的所有元素并设置值
$(document).ready(function() {$('#mymodal').on('隐藏', function() {$(':input', this).val('');});});
I have this modal
<form id="contactModal">
<div id="mymodal2" class="" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<span class="modal-title th2" id="lblModalLabel" name="lblModalLabel">Contact</span>
</div>
<div class="modal-body">
What I want is to clear modal every time its closed so I wrote a script like this:
function clear() {
$("#txtNombreCompleto").val("");
$("#txtNombreEmpresa").val("");
$("#exampleInputEmail2").val("");
$("#dropOficina").val("");
$("#txtTelefono").val("");
$("#txtMensaje").val("");
}
$('#mymodal').on('hidden', function(){
$.clear(this)
});
So my inputs inside the modal are something like this:
<input type="text" class="form-control" id="txtNombreCompleto" name="txtNombreCompleto" placeholder="Nombre completo">
But when I close the modal, it doesn´t run the function, how can I run the function when the modal closes? Regards
Simply call clear();
inside bootstrap modal hide.bs.modal
event(or hidden.bs.modal
) handler or set the function as callback. Also put your code inside document ready handler
for attaching event handler after loading the page.
$(document).ready(function() {
$('#mymodal').on('hidden', function() {
clear()
});
});
or get all form all elements inside modal and set value
$(document).ready(function() {
$('#mymodal').on('hidden', function() {
$(':input', this).val('');
});
});
这篇关于关闭模态后清除模态字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!