2010-08-06 84 views
7

有没有办法重新加载选定的选项卡我知道有.load()函数。但它想要一个标签索引,我似乎没有看到一种方法来抓住选定的标签ID。JQuery UI - 重新加载选定的选项卡?

+0

所以你的问题实在是如何让所选择的选项卡的索引? – 2010-08-08 14:36:41

+0

这会解决我的问题,是的。 – Renari 2010-08-08 17:29:38

回答

24

更新:在jQuery 1.9中,selected选项重命名为active。请参阅attomman's answer.

要获取当前选定的索引,请使用tabs('option','selected')函数。

例如,如果你有一个按钮#button(和#tabs元素制作成标签),你想要得到的指标,做到以下几点:

$("#button").click(function() { 
    var current_index = $("#tabs").tabs("option","selected"); 
}); 

这里有一个演示:http://jsfiddle.net/sVgAT/


要回答标题所示,在jQuery的1.8的问题以及更早版本,你会怎么做:

var current_index = $("#tabs").tabs("option","selected"); 
$("#tabs").tabs('load',current_index); 

而在jQuery的1.9或更高版本,你会怎么做:

var current_index = $("#tabs").tabs("option","active"); 
$("#tabs").tabs('load',current_index); 
+0

谢谢,那正是我正在寻找的。 – Renari 2010-08-08 19:12:57

+0

此解决方案的问题是,它会重新加载未选定的选项卡两次(服务器上有两次点击)。 – 2012-09-24 21:11:00

+2

不幸的是,截至最新版本的jQuery UI,这不再有效。根据attomman的回答,你必须用''active''替换''选定的''''。 – aroth 2013-04-22 01:30:28

1

我设法得到这个工作,但现在加载第一个选项卡的两倍,当它处于非活动状态。如果任何人有任何更多的建议,将不胜感激。 console showing a cancelled request to the server

您可以在上面看到,该请求向服务器发出两次,但第一个请求成功,因此它取消了第二个请求。不知道这是否是一个问题..??但是我没有被看到它在控制台放心

constructor: (@element) -> 
    @tabIndex = 0 
    @tabs = @element.find('#tabs') 
    @tabs.tabs 
     spinner: "Loading.." 
     cache: false 
     ajaxOptions: 
     cache: false 
     error: (xhr, status, index, anchor) -> 
      $(anchor.hash).html "Couldn't load this tab. We'll try to fix this as soon as possible." 

    #add custom load to make sure they always reload even the current tab when clicked 
    @element.find('ul li a').click (e) => 
     if @tabIndex is @tabs.tabs('option', 'selected') 
     @tabs.tabs 'load', @tabs.tabs('option', 'selected') 
     @tabIndex = @tabs.tabs('option', 'selected') 
+0

这应该是一个单独的问题,所以人们可以正确回答:) – 2012-09-25 12:56:21

2

如果有人绊倒了这个问题,并像我一样使用jQuery EasyUI的标签,西门子上面的回答将不起作用。相反,刷新标签,使用:

var tab = $('#tt').tabs('getSelected'); 
tab.panel('refresh'); 

哪里tt是通过

<div id="tt" class="easyui-tabs"> 
+0

它确实适用于新版本Easy UI,谢谢 – JacobChan 2013-09-10 08:35:26

0

下载这个插件创建的Cookie中的选项卡组的id

http://plugins.jquery.com/cookie/

插入jQuery cookie到你的页面

<script src="jquery.cookie.js"></script> 

,并补充一点:

$(".tabs").click(function() { 
    //getter , get the active tab 
    var active = $("#tabs").tabs("option", "active"); 
    $.cookie("tabs_selected", active); 
}); 

var cookieValue = $.cookie("tabs_selected"); 
// setter, set the active tab stocked 
$("#tabs").tabs("option", "active",cookieValue); 
相关问题