2016-10-14 50 views
1

我有一个简单的AJAX请求,它根据表中的某些选择更新一些PHP会话变量。如果AJAX请求发生错误,我会更改它们所选表格的颜色(如果它们选择“是”,则通常会变为绿色,如果选择“否”,则会变为红色)。这一切都很好。在AJAX请求错误中显示引导警报

我现在想在屏幕上显示的可取消引导警报之一,也是一样,在他们的例子:

<div class="alert alert-warning alert-dismissible" role="alert"> 
 
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span> 
 
    </button> 
 
    <strong>Warning!</strong> Better check yourself, you're not looking too good. 
 
</div>

,但我不知道我怎么会引起这会在AJAX请求发生错误时显示。这里是我的脚本:

$(document).ready(function() { 
 
    $('button.btn-success').click(function() { 
 
    var refreshItemID = $(this).closest('tr').attr('id'); 
 
    console.log(refreshItemID); 
 
    // Create a reference to $(this) here: 
 
    $this = $(this); 
 
    $.post('updateSelections.php', { 
 
     refreshItemID: refreshItemID, 
 
     selectionType: 'yes' 
 
    }, function(data) { 
 
     data = JSON.parse(data); 
 
     if (data.error) { 
 
     console.log(data); 
 
     console.log('something was wrong with the ajax request'); 
 
     $this.closest('tr').addClass("warning"); 
 
     return; // stop executing this function any further 
 
     } else { 
 
     console.log('update successful - success add class to table row'); 
 
     $this.closest('tr').addClass("success"); 
 
     $this.closest('tr').removeClass("danger"); 
 
     //$(this).closest('tr').attr('class','success'); 
 
     } 
 
    }).fail(function(xhr) { 
 
     console.log('ajax request failed'); 
 
     // no data available in this context 
 
     $this.closest('tr').addClass("warning"); 
 
    }); 
 
    }); 
 
});

你可以看到它增加了一个警告类的表行,如果有一个错误:

$this.closest('tr').addClass("warning"); 

,但我不知道如何扩展这也添加了一个可以接受的警报(以及如何控制该警报的位置)?

+0

使用这种自举通知[点击此处](http://bootstrap-notify.remabledesigns.com/) –

回答

0

例如将上述警报的html代码添加到您的表格的上方,并将style="display:none"添加到警示div。还要设置一个id,如id="alert_ajax_error"到该div。

现在失败的回调应该是这样的:

.fail(function(xhr) { 
    console.log('ajax request failed'); 
    // no data available in this context 
    $this.closest('tr').addClass("warning"); 

    //make alert visible 
    $("#alert_ajax_error").show(); 
}); 

在成功躲警报的情况下,与$("#alert_ajax_error").hide();

1

设置提醒DIV默认显示:none和Ajax调用成功/文件中/错误,您可以使用下面的代码显示警报。

$(document).ready(function() { 
 

 

 
    $('button#alertshow').on('click', function() { 
 
    var msg_type = $("#msgtype").val(); 
 
    ShowAlert(msg_type, 'Message Content', msg_type); 
 
    }); 
 

 

 
    function ShowAlert(msg_title, msg_body, msg_type) { 
 
    var AlertMsg = $('div[role="alert"]'); 
 
    $(AlertMsg).find('strong').html(msg_title); 
 
    $(AlertMsg).find('p').html(msg_body); 
 
    $(AlertMsg).addClass('alert-' + msg_type); 
 
    $(AlertMsg).show(); 
 
    } 
 

 
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id="alertshow">Show alert</button> 
 
<select id="msgtype"> 
 
    <option value="warning">Warning</option> 
 
    <option value="info">Info</option> 
 
    <option value="success">Success</option> 
 
    <option value="danger">Danger</option> 
 
</select> 
 
<div class="alert alert-dismissible" role="alert" style="display:none;"> 
 
    <strong>Warning!</strong> 
 
    <p>Better check yourself, you're not looking too good.</p> 
 
</div>