3

我已经和这个摔跤了一段时间了。

我想在有人更改手风琴之前有一个确认()。

我努力了:

$(document).ready(function() {

    var edited = false;

    $(".accordion-me").accordion({
        autoHeight: false,
        navigation: true,
        changestart: function(event, ui) {
            if (edited) {
                if (!confirm("You have unsaved changes. Do you want to navigate away?") {
                    event.preventDefault();
                    event.stopPropagation();
                    event.stopImmediatePropagation();
                    return false;
                }
            }
        }
    });
});

带着一点喜悦!我也尝试过这样的事情

   $(".accordion-me h3").each(function() {

                    $(this).unbind("click");

                    $(this).click(function(e) {

                        if (confirm("You have unsaved changes! Do you want to navigate away?")) {
                            $(this).unbind("click");
                            $(".accordion-me").accordion({
                                autoHeight: false,
                                navigation: true,
                                changestart: function(event, ui) {
                                    if (edited) {
                                        if (!confirm("You have unsaved changes. Do you want to navigate away?") {
                                            event.preventDefault();
                                            event.stopPropagation();
                                            event.stopImmediatePropagation();
                                            return false;
                                        }
                                    }
                                }
                            });                            
                            $(this).click();
                        }
                    });
                });

但又没有喜悦。

任何帮助将不胜感激。

干杯

4

2 回答 2

4

创建手风琴时使用空事件,这将允许您使用 jQuery .click 函数管理手风琴的单击事件。然后,您可以处理确认框并仅在确认后才允许执行手风琴点击事件。

$(document).ready(function()
{
    var edited = false,
        accordion_me = $('.accordion-me');

    // activate the accordion, but with an empty event
    accordion_me.accordion({
        autoHeight: false,
        navigation: true,
        event: ''
    });

    // here's the new accordion event
    $('.accordion-me h3').click(function()
    {
        // find the index of the event being called
        var i = $('.accordion-me h3').index(this);

        // if we have unsaved changes and do not confirm, stop accordion execution      
        if (edited && !confirm('You have unsaved changes. Do you want to navigate away?'))
        {
            return false;
        }

        // continue with the accordion execution. Activate the requested event index.
        accordion_me.accordion('activate', i);

        return false;
    });
});

如果您的手风琴是可折叠的(就像我的那样),您的手风琴仍然可以像以前一样工作。另外,如果您只有 1 个手风琴,我建议使用 id 来调用它,而不是 .accordion-me 类,这样可以节省一些开销。如果还需要用一个类来调用它,在它前面加上一个html标签,即div.accordion-me。

于 2011-09-19T19:12:29.297 回答
0

您必须将其绑定到锚标记上的点击事件。例如,如果您的标题链接是:

<a href="#" class="accordionHeaderLink">header 1</a>

代码将是(也在 document.ready 函数中)

$('.accordionHeaderLink').click(function(){
    if (!confirm("You have unsaved changes. Do you want to navigate away?")) {
        return false;
    }
});
于 2011-07-08T19:42:47.063 回答