具有data-dismiss =“alert”属性的下方按钮显示Bootstrap 2模式对话框,以便用户确认是否关闭警报。
现在在用户甚至在模态确认弹出窗口中选择任何内容之前,“关闭UI,而不是弹出窗口”的“提醒”警报关闭
。如何根据用户在模态弹出对话框中选择的内容,使Bootstrap“alert”(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');
});
});
});
});
答案 0 :(得分:2)
您应该在AJAX通话的$('#deleteNotificationConfirmModal').modal('hide');
功能上调用success
。
这是因为您正在使用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');
}
});
});