Jquery UI手风琴 - 取消更改

3

我已经苦思冥想了一段时间。

我希望在某人更改手风琴之前进行确认(confirm())。

我尝试过:

$(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();
                        }
                    });
                });

但是仍然没有任何进展。

非常感谢您的帮助。

谢谢。

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;
    });
});

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


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;
    }
});

我在使用ui dialog confirmation时遇到了类似的问题。即使将其绑定到点击事件上,我仍然无法让它工作。当ui dialog出现时,手风琴也会发生变化。 - Dan Stotmeister

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接