jquery - 根据模式确认对话框选择关闭 Bootstrap 警报

标签 jquery asp.net-mvc twitter-bootstrap

下面具有 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/

相关文章:

javascript - 在没有选择器先决条件的情况下创建自定义 jQuery 函数

javascript - 如何在 jQuery 中仅隐藏和显示而不使用任何显示函数

c# - 无法在Windows主机上上传asp.net mvc3网站

javascript - MVC Controller 调用中未填充内部对象

javascript - Bootstrap 响应式导航栏链接对齐和样式问题

jQuery 复选框错误

jquery - 有 jQuery jEditable 多选插件吗?

asp.net-mvc - 如何将 MVC 5 IdentityModels.cs 移动到单独的程序集中

php - 导出 Bootstrap DOM - 通过 DOMPDF 将 HTML 导出为 PDF

css - 我可以使用 twitter bootstrap 在同一个网站上使用不同数量的网格列吗?