【发布时间】:2016-10-14 10:49:22
【问题描述】:
我有一个简单的 AJAX 请求,它根据表格中的一些选择更新一些 PHP 会话变量。如果 AJAX 请求出现错误,我会更改他们选择的表格行的颜色(如果他们选择“是”,通常会变为绿色,如果选择“否”,则通常会变为红色)。这一切都很好。
我现在还想在屏幕上显示一个可关闭的 Bootstrap 警报,就像在他们的示例中一样:
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</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");
但我不确定如何扩展它以添加可关闭的警报(以及如何控制该警报的位置)?
【问题讨论】:
-
使用这个引导通知CLICK HERE
标签: javascript jquery ajax twitter-bootstrap