jQuery 4 秒后引导模式关闭或用户单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18730284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bootstrap modal close after 4 seconds or user click
提问by Alex
How would I set a timeout for a bootstrap modal? After getting the ajax data back that the message returned by php contains the term success
, I want to give the user the option to close the window. However, I also just want to have a 4 second count down. Currently the second the success message comes back the modal hides itself.
我将如何为引导模式设置超时?在获取 ajax 数据后,php 返回的消息包含术语success
,我想给用户关闭窗口的选项。但是,我也只想倒计时 4 秒。目前,第二个成功消息返回模态隐藏自身。
$('#forgotform').submit(function (e) {
"use strict";
e.preventDefault();
$('#forgotsubmit').button('loading');
var post = $('#forgotform').serialize();
var action = $('#forgotform').attr('action');
$("#message").slideUp(350, function () {
$('#message').hide();
$.post(action, post, function (data) {
$('#message').html(data);
document.getElementById('message').innerHTML = data;
$('#message').slideDown('slow');
$('#usernamemail').focus();
if (data.match('success') !== null) {
$('#forgotform').slideUp('slow');
$('#forgotsubmit').button('complete');
$('#forgotsubmit').click(function (eb) {
eb.preventDefault();
$('#forgot-form').modal('hide');
});
setTimeout($('#forgot-form').modal('hide'), 10000);
} else {
$('#forgotsubmit').button('reset');
}
});
});
});
回答by James Lawruk
When calling setTimeout(), wrap your command in an anonymous function. Otherwise the command will be executed immediately.
调用 setTimeout() 时,将您的命令包装在匿名函数中。否则命令将立即执行。
setTimeout(function() {$('#forgot-form').modal('hide');}, 4000);
回答by ebentil
setTimeout(function(){
$('#Modal').modal('hide')
}, 4000);
//where modal's id is 'Modal'
//其中模态的id是'Modal'
回答by Shashank Malviya
$('#submit1').click(function(){
setTimeout("$('#myModal').modal('hide');",3000);
});
this is working for popup delaying 3 seconds in closing.
please check with the $('#submit1')
for this click I have written the code.
这适用于在关闭时延迟 3 秒的弹出窗口。请检查$('#submit1')
此点击我已经编写了代码。
回答by Subhash Patel
The following code is used for hiding the model on an onClick
event. Use the classname for the onClick
listener and the modal id as the selector to hide.
以下代码用于在onClick
事件上隐藏模型。使用onClick
监听器的类名和模式 id 作为选择器来隐藏。
$('.class_name').on('click',function(){
$('#modal_id').modal('hide');
});