2017-02-20 49 views
0

我正在使用BS模式在时事通讯表单上显示反馈。 它使用AJAX和PHP在数据库中插入电子邮件并返回STATUS。Bootstrap模态,反馈不会消失

PHP和AJAX响应工作正常,验证和一切,但是,当我调用模态使用JS来显示反馈,模态不再关闭,不能被解雇。

模态:

<div id="myModal" class="modal fade" role="dialog"> 
    <div class="modal-dialog"> 
    <!-- Modal content--> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal">&times;</button> 
     <h4 class="modal-title">ENGINE|Sistemas</h4> 
     </div> 
     <div class="modal-body"> 
     <p id="response"></p> 
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-primary" data-dismiss="modal">Fechar</button> 
     </div> 
    </div> 
    </div> 
</div> 

而且JS:

$(function() 
{ 
    var form = $('#newsletter-form'); 
    $(form).submit(function(e) 
    { 
    e.preventDefault(); 

    var formData = $(form).serialize(); 

    $.ajax({ 
     type: 'POST', 
     url: $(form).attr('action'), 
     data: formData 
    }) 

    // ---------------------------------------- 
    // Success 
    // ---------------------------------------- 
    .done(function(response) 
    { 
     $('#myModal').modal('show'); 
     $('#myModal, #response').replaceWith(response); 
     $('#newsletter-email').val(''); 
    }) 

    // ---------------------------------------- 
    // Error 
    // ---------------------------------------- 
    .fail(function(data) 
    { 
     if (data.responseText !== '') 
     { 
     $('#myModal').modal('show'); 
     $('#myModal, #response').replaceWith(data.responseText); 
     } 
     else 
     { 
     alert('Ocorreu um erro! Seu email não foi cadastrado.'); 
     } 
    }); 
    }); 
}); 

我不明白为什么寿模式完美展现了反应,但根本不会关闭了...

任何帮助?

回答

0

你是否在控制台中看到错误的js?如果没有,试试这个:

$("#myModal").on('hidden', function(){ 
    $("#myModal").remove(); 
    $('.modal-backdrop').remove(); 
}); 
+0

我刚foundout使得显示所述响应是导致误差修改。 '('#myModal')。modal('show');'工作正常,但如果我把响应'$('#myModal,#response')。replaceWith(data.responseText);'它停止工作! –

+0

而且我已经试着做你发布的内容,而不是工作。 –

0

那么,现在它的工作。

我不知道为什么,但反应阻止了模态的行为正确。

解决只是改变:

$('#myModal').modal('show'); 
    $('#myModal, #response').replaceWith(data.responseText); 

$('#myModal').modal('show'); 
    $('#response').text(data.responseText); 

=)