【发布时间】:2014-07-22 23:00:48
【问题描述】:
下方具有 data-dismiss="alert" 属性的按钮会显示 Bootstrap 2 模式对话框,以便用户确认是否关闭警报。
现在引导程序“警报”(UI 上的 div,而不是弹出窗口)在用户选择模式确认弹出窗口中的任何内容之前关闭。
如何根据用户在模式弹出对话框中选择的内容来关闭或保持引导引导“警报”(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');
});
});
});
});
【问题讨论】:
-
你能创建一个小提琴来帮助解决问题吗? jsfiddle.net
标签: jquery asp.net-mvc twitter-bootstrap