【问题标题】:Close Bootstrap Alert based on modal confirm dialog choice根据模态确认对话框选择关闭引导警报
【发布时间】: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


【解决方案1】:

您应该在 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');
        }
    });
});

【讨论】:

  • 好吧,也许我应该这样做,但问题是根据用户选择的确认对话框选项让 UI 上的警报保持打开或关闭。
  • 我指的“警报”不是弹出窗口,而是引导程序“警报”,它只是 UI 上的一个 div。
  • 您是否尝试过绑定到close.bs.alert?请参阅文档:getbootstrap.com/javascript/#alerts
  • 刚刚尝试绑定到 close.bs.alert - 它似乎绑定与关闭相同。如果用户在模式弹出窗口上选择取消,我需要一种取消 UI 上的关闭并保持警报可见的方法。问题是,一旦您单击按钮,警报就会关闭,甚至在显示模式弹出窗口之前。
  • 更新了我的代码,使其更独立于按钮点击......看起来你必须使用preventDefault() - 请参阅上面的更新代码并以小提琴为例。代码可能不是 100% 完美,但它会让你克服障碍(我相信)
猜你喜欢
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多