Jquery tabs - Showing a confirmation before activa

2020-04-16 06:18发布

I'm using jquery UI's tabs. When a user switches tabs, I'm checking for unsaved changes, and prompting the user accordingly. However, before the user even clicks Yes or No, the tab loads anyway. Does anyone know how I can get my function to NOT return anything until after the result has come back from my fancybox dialog?

You can see I've tried a couple of different methods to return my boolean value in CheckSomething().

$("#myTabs").tabs({ 
    select:
        function (event, ui) {
            return CheckSomething();
        },

    show: 
        function (event, ui) { 
            //do some stuff
        }
});

function CheckSomething() {
    var loadMyTab = true;  //If I set this to false, then it always returns false.
    if (myCondition) {
        //Show a FancyBox prompt.

        if (fancyYes) {
            //return true;
            loadMyTab= true;
        }
        else {
            //return false;
            loadMyTab = false;
        }
    }
    else {
        //return true;
        loadMyTab = true;
    }
    return loadMyTab;
}

2条回答
Root(大扎)
2楼-- · 2020-04-16 06:25

With a standard confirm instead of a fancy is working can it be a fast solution? http://jsfiddle.net/nuywj/

查看更多
老娘就宠你
3楼-- · 2020-04-16 06:44

I ended up going w/ a standard javascript confirm box instead of FancyBox. That solved my problem, because Confirm will block code from being executed:

function CheckSomething() {
    if (myCondition) {
        return confirm("Are you sure you want to change tabs without saving?");
    }
    else {
        return true;
    } 
}
查看更多
登录 后发表回答