bootstrap modal close after 4 seconds or user click

独自空忆成欢 提交于 2019-11-29 01:08:48

问题


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.

$('#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');
            }
        });
    });
});

回答1:


When calling setTimeout(), wrap your command in an anonymous function. Otherwise the command will be executed immediately.

setTimeout(function() {$('#forgot-form').modal('hide');}, 4000);



回答2:


setTimeout(function(){
  $('#Modal').modal('hide')
}, 4000);

//where modal's id is 'Modal'




回答3:


$('#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.




回答4:


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.

$('.class_name').on('click',function(){
    $('#modal_id').modal('hide');
});


来源:https://stackoverflow.com/questions/18730284/bootstrap-modal-close-after-4-seconds-or-user-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!