开发者

window.location.hash issue when working with jQuery tabs

I have three forms on a page within three separate tabs. Each tab is uniquely identified as necessary when using jQuery tabs.

When I submit a form I display a thank you message and then redirect the page back to the initial page with the three tabs.

On setting the location back to the original page I want it to automatically go to the tab that the form was submitted from.

I hav开发者_开发技巧e tried the following:

setTimeout(setToDefault,2000); 

function setToDefault()
{  window.location = window.location.hash="tabs-28";  }

This changes the address in the url bar but the page doesn't go anywhere. Any ideas?


You're unclear if you're already on the 'original' page when you run that JavaScript, but you're trying to set window.location without a URL, so I am assuming you are already on that correct page and are trying to display another tab:

If you're using jQuery Tabs, set the hash for history, then use the select() method:

window.location.hash = "tabs-28";   // only returns 'tabs-28' back, which is
                                    // why setting it to window.location then
                                    // doesn't work

var index = ???;                    // ??? = whatever the tab index is
myTabElement.tabs("select", index)  // use the jQuery Tab library to switch to
                                    // the desired tab

If you're not on the original page that has the tabs, then on the 'thank you' page:

window.location.hash = "originalpage.html#tabs-28";

And on then back on the 'original' page:

jQuery(function({ 
    if (window.location.hash != '') {
        var myTabElement = jQuery('#whateverTabElementIs'),
            index = ???; 
            // ??? = however you figure the index based on current window.location.hash
        myTabElement.tabs("select", index);
    } 
}));

OR if you want when you're initializing the tabs element in the first place

jQuery( "#whateverTabElementIs" ).tabs({ selected: ??? });
// ??? = however you figure the index based on current window.location.hash
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜