下面具有 data-dismiss="alert"属性的按钮会显示 Bootstrap 2 模式对话框,以便用户可以确认是否关闭警报。
现在Bootstrap“警报”(这是 UI 上的一个 div,而不是弹出窗口)在用户在模式确认弹出窗口中选择任何内容之前就关闭了。
如何根据用户在模式弹出对话框中的选择让 Bootstrap“警报”(div) 关闭或保持打开状态?
<div class="span8 see-all-notifications">
<div id="notification-list" class="module main-slot-c">
@foreach (var notification in Model.Notifications)
{
<div class="noti-alert">
<div class="alert alert-info">
<button type="button" class="close" data-dismiss="alert"
data-notificationid="@notification.NotificationId"
data-contactpointid="@notification.ContactPointId"
data-apiurl="@Url.Content("~/api/account/")">
x
</button>
<h5>
<i class="icon-file @notification.EventCategoryIdentifier"></i> @notification.Description <span>Reported at @notification.RaisedDate</span>
</h5>
<div class="noti-collapse">
<div class="noti-collapse-inner">
@notification.Body
</div>
</div>
</div>
</div>
}
</div>
</div>
<div id="deleteNotificationConfirmModal" class="modal hide fade in" style="display: none; ">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Confirm Deletion of Notification</h3>
</div>
<div class="modal-body">
<h4>Are you sure you want to delete the notification?</h4>
</div>
<div class="modal-footer">
<a id="notificationDeleteConfirm" href="#" class="btn btn-success">Delete Notification</a>
<a href="#" class="btn" data-dismiss="modal">Close</a>
</div>
</div>
这是 javascript 文件的相关部分
// This function wires up the code to "close" the notification (set status to "Read")
$('.noti-alert').each(function(){
$(this).bind('close', function () {
var notificationId = $(this).find('button').data('notificationid');
var contactpointId = $(this).find('button').data('contactpointid');
var url = $(this).find('button').data('apiurl');
$("#deleteNotificationConfirmModal").modal('show');
$('#notificationDeleteConfirm').on('click', function () {
//"Delete" the notification --> CloseNotification method in api controller
$.ajax({
type: "POST",
url: url,
data:{
notificationid: notificationId,
contactpointid: contactpointId
},
success: function (data) { },
error: function (err) {
alert("error" + err.status + " " + err.statusText);
}
});
$('#deleteNotificationConfirmModal').modal('hide');
});
});
});
});
编辑:这是我要工作的最终代码。我需要调用 e.preventDefault
,然后通过调用 ajax 成功回调中的 thisAlert.remove();
方法来关闭“警报”
// This function wires up the code to "close" the notification (set status to "Read")
$('.noti-alert').each(function(){
$(this).bind('close.bs.alert', function (e) {
e.preventDefault();
var notificationId = $(this).find('button').data('notificationid');
var contactpointId = $(this).find('button').data('contactpointid');
var url = $(this).find('button').data('apiurl');
$("#deleteNotificationConfirmModal").modal('show');
// set a reference to the notification so we can remove it in the ajax call below
var thisAlert = $(this);
$('#notificationDeleteConfirm').on('click', function () {
//"Delete" the notification --> CloseNotification method in api controller
$.ajax({
type: "POST",
url: url,
data:{
notificationid: notificationId,
contactpointid: contactpointId
},
success: function (data) {
$('#deleteNotificationConfirmModal').modal('hide');
thisAlert.remove();
},
error: function (err) {
alert("error" + err.status + " " + err.statusText);
}
});
});
// bind modal's close button click event
$('.notificationDeleteCancel').on('click', function () {
// close modal
//alert('cancel');
$("#deleteNotificationConfirmModal").modal('hide');
});
});
});
});
最佳答案
您应该在 AJAX 调用的 success
函数上调用 $('#deleteNotificationConfirmModal').modal('hide');
。
这是因为您正在使用 ajax 调用(异步)
例如:
success: function (data) {
$('#deleteNotificationConfirmModal').modal('hide');
},
更新代码如下: <强> See fiddle
// This function wires up the code to "close" the notification (set status to "Read")
$('.alert-info').each(function () {
// show confirmation dialog when closing alert message
$(this).bind('close.bs.alert', function (e) {
e.preventDefault();
$("#myModal").modal('show');
});
});
// bind modal's close button click event
$('.notificationDeleteCancel').on('click', function () {
// close modal
alert('cancel');
});
// bind modal's delete notification button click event
$('.notificationDeleteConfirm').on('click', function () {
// do ajax
alert('delete');
//"Delete" the notification --> CloseNotification method in api controller
$.ajax({
type: "POST",
url: "/echo/json/",
data: 'mydata',
cache: false,
success: function(json){
$("#myModal").modal('hide');
$('.alert-info').alert('close');
},
error: function (err) {
alert( 'error');
}
});
});
关于jquery - 根据模式确认对话框选择关闭 Bootstrap 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24001106/