简体   繁体   English

单击/激活jQuery UI选项卡时触发函数?

[英]Trigger a function when a jQuery UI tab is clicked/made active?

I'm using jQuery tabs and the content in one of the tabs is retrieved via an AJAX call. 我正在使用jQuery选项卡,并通过AJAX调用检索其中一个选项卡中的内容。 But I don't want to trigger the call until the tab is clicked (made active) because the user might not necessarily click it. 但是我不想在单击选项卡(激活)之前触发调用,因为用户可能不一定要单击它。 What's the best way to do this? 最好的方法是什么?

jQuery UI provides select and show events for the tabs, but these fire when any tab is selected or shown, not just a specific one (as far as I know). jQuery UI为选项卡提供了selectshow事件,但是当选择或显示任何选项卡时,这些事件都会触发,而不仅仅是特定选项卡(据我所知)。

I've also tried assigning a click event to the specific tab's ID: 我还尝试将click事件分配给特定选项卡的ID:

$("#my-tab").click(function(){
...
     $.post(URL, function(data){
          ...
     });
...
}

But this seemingly has no effect and the call is never triggered. 但这似乎没有任何影响,呼叫永远不会被触发。

The ticked solution doesn't work any more because jquery-ui has changed . 勾选的解决方案不再起作用,因为jquery-ui已经改变 Also you need to call the tabs function on the entire tab-set, not just one tab. 您还需要在整个选项卡集上调用选项卡功能,而不仅仅是一个选项卡。 And ui.index needs to be replaced. 并且ui.index需要被替换。 If the 2nd panel has the id "tab2" then you can use: 如果第二个面板的ID为“tab2”,那么您可以使用:

$("#tabs").tabs({
    activate: function(event, ui) {
       if (ui.newPanel.selector == "#tab2") {
            alert("tab 2 selected");
       }
    }
});

Ui-tabs provide a function which is triggered when you select a tab. Ui-tabs提供了一个在选择选项卡时触发的功能。

$( ".yourTab" ).tabs({
   select: function(event, ui) {
        if(ui.index == myTabIndex) {
            $.post(URL, function(data){
              ...
            });
        }
   }
});

Note Use activate for new versions. 注意对新版本使用activate

reference 参考

You may try something like this: 你可以尝试这样的事情:

/*tab placeholder*/.tabs({
    select: function(event, ui) {
       if(ui.item.id === "my-tab"){
           //explicitly trigger mouse click on tab
           $(ui.item).trigger('click');
       }
    }
});
//------------------
$("#my-tab").click(function(){
    alert('something')
});

The select event has a ui parameter where you can check the index of the selected tab. select事件有一个ui参数,您可以在其中检查所选选项卡的index This SO question has many examples: jQuery UI Tabs Get Currently Selected Tab Index 这个SO问题有很多例子: jQuery UI选项卡获取当前选定的选项卡索引

You can then do the ajax call when you have checked the index of the tab. 然后,在检查选项卡的index ,可以执行ajax调用。

EDIT: You also have many examples in the official Jquery UI doc here: http://docs.jquery.com/UI/Tabs 编辑:您在官方Jquery UI文档中也有很多示例: http//docs.jquery.com/UI/Tabs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM