使用 Bootstrap 模态 (Bootbox) 进行不显眼的 JavaScript 确认

unobtrusive javascript confirm using bootstrap modal (bootbox)

提问人:pennstatephil 提问时间:1/22/2014 更新时间:1/25/2014 访问量:2208

问:

我的 MVC 应用程序中有一个链接:

<a class="btn  btn-default" confirm="Are you sure you want to delete the selected group? This action cannot be undone!" data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#TabsId-5" href="/Groups/DeleteGroup/38" id="btnDelGrp"><i class="glyphicon glyphicon-remove"></i> Delete Selected Group</a>

和以下 JS:

$(document).ready(function ()
{
$("[confirm]").each(function () {
    $(this).click(function (e) {
        e.preventDefault();
        var action = function () { alert("no action"); };
        switch ($(this).prop('tagName'))
        {
            case 'A':
                var url = $(this).attr("href");
                action = function () { document.location.href = url; };
                break;
            case 'BUTTON':
                var form = $(this).closest('form');
                if ($(this).attr("name") != "" && $(this).attr("value") != "")
                {
                    var foundIndex = $(form).attr('action').indexOf($(this).attr("name"));
                    if (foundIndex == -1) //don't add the same param over and over
                    {
                        $(form).attr('action', $(form).attr('action') + "?" + $(this).attr("name") + "=" + $(this).attr("value"));
                    }
                    else //else replace it
                    {
                        $(form).attr('action', $(form).attr('action').substring(0, foundIndex) + $(this).attr("name") + "=" + $(this).attr("value"));
                    }
                }
                action = function () { form.submit(); };
                break;
        }
        bootbox.confirm($(this).attr("confirm"), function (result) {
            if (result)
                action();
        });
    });
});
});

我遇到了一个问题,即无论如何,对话框显示后链接都会触发。不显眼的 ajax 似乎不尊重 .我知道使用该选项有一种解决方法,但这使用通用的 js 警报弹出窗口。有没有办法让这两件事一起工作?preventDefaultdata-ajax-confirm

javascript jquery ajax twitter-bootstrap unobtrusive-javascript

评论


答:

1赞 TBohnen.jnr 1/22/2014 #1

你应该能够破解它说function="return false;"

<a class="btn  btn-default" confirm="Are you sure you want to delete the selected group? This action cannot be undone!" data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#TabsId-5" href="#" data-href="/Groups/DeleteGroup/38" id="btnDelGrp" function="return false;"><i class="glyphicon glyphicon-remove"></i> Delete Selected Group</a>

或者更好的是,只需使用 data-href 而不是 href:

<a class="btn  btn-default" confirm="Are you sure you want to delete the selected group? This action cannot be undone!" data-ajax="true" data-ajax-method="GET" data-ajax-mode="replace" data-ajax-update="#TabsId-5" href="#" data-href="/Groups/DeleteGroup/38" id="btnDelGrp"><i class="glyphicon glyphicon-remove"></i> Delete Selected Group</a>

和 js:

$(document).ready(function ()
{
$("[confirm]").each(function () {
    $(this).click(function (e) {
        e.preventDefault();
        var action = function () { alert("no action"); };
        switch ($(this).prop('tagName'))
        {
            case 'A':
                var url = $(this).attr("data-href");
                action = function () { document.location.href = url; };
                break;
            case 'BUTTON':
                var form = $(this).closest('form');
                if ($(this).attr("name") != "" && $(this).attr("value") != "")
                {
                    var foundIndex = $(form).attr('action').indexOf($(this).attr("name"));
                    if (foundIndex == -1) //don't add the same param over and over
                    {
                        $(form).attr('action', $(form).attr('action') + "?" + $(this).attr("name") + "=" + $(this).attr("value"));
                    }
                    else //else replace it
                    {
                        $(form).attr('action', $(form).attr('action').substring(0, foundIndex) + $(this).attr("name") + "=" + $(this).attr("value"));
                    }
                }
                action = function () { form.submit(); };
                break;
        }
        bootbox.confirm($(this).attr("confirm"), function (result) {
            if (result)
                action();
        });
    });
});
});

评论

0赞 pennstatephil 1/22/2014
在“a”标签上使用非ajax“confirm”的任何地方使用data-href不会中断吗?
0赞 TBohnen.jnr 1/22/2014
是的,但你会为他们不留下 href 吗?非 ajax 和 ajax href 需要看起来一样是有原因的吗?
0赞 pennstatephil 1/22/2014
不,他们没有......所以你建议(伪代码)如果data-href存在,就使用它,否则使用href?我首先使用它的原因是我正在使用一个 MVC 助手——ActionLink
0赞 TBohnen.jnr 1/22/2014
啊,现在这更有意义了(助手部分)。老实说,我没有一起尝试过这两者,在这种情况下可以不使用助手吗?现在是晚上20:30,我要回家了,但如果你还没有找到答案,我早上会帮你看看吗?
0赞 pennstatephil 1/22/2014
是的,我可以像我们写的那样粘贴链接的 HTML,然后为此抛出帮助程序。我会试一试,看看这是否有效:)
1赞 Travis Watson 1/25/2014 #2

我会停止使用不显眼的东西,因为它似乎没有达到它的主要目标:不显眼!

请改用 jQuery ajax 方法。下面是一个小提琴示例:http://jsfiddle.net/VM4HH/

下面是使定位功能起作用的代码片段:

$(function() {
    $('body').on('click', 'a.confirm', function(e) {
        e.preventDefault();
        var $tar = $(e.target);

        customConfirm($tar.data('confirm'), function() {

            var $contentTarget =  $('#' + $tar.data('custom-ajax-target-id'));

            $contentTarget.html('Loading...');

            $.ajax({
                url: $tar[0].href,
                // Following two lines for the JSFiddle demo only
                type: 'POST',
                data: { html: '<h1>' + $tar.html() + '</h1>', delay: 1 }
            }).success(function(data) {
                $contentTarget.html(data);
            });
            // You'll want to attach error handlers here to your promise
        });
    });
});

下面是示例 HTML:

<a href="/echo/html/" data-confirm="Go to Google?" data-custom-ajax-target-id="content"  class="confirm">Googs</a><br />
<a href="/echo/html/" data-confirm="Go to Yahoo?" data-custom-ajax-target-id="content" class="confirm">Yahoo</a><br />

评论

0赞 pennstatephil 1/28/2014
受这篇文章的启发,我最终修改了原始代码。这是我最终得到的: 案例 'A': var url = $(this).attr(“href”);if (!$(this).attr(“ajax-replaceTargetId”)) { action = function () { document.location.href = url; }; } else { action = function () { $.ajax({ url: url }) .done(function (html) { $('#' + $(this).attr(“ajax-replaceTargetId”)).html(html); } } break;
0赞 pennstatephil 1/28/2014
由于某种原因,我无法将其呈现为代码,对不起。