Detecting tab getting selected under jQueryUI

2019-07-09 09:24发布

I am trying to detect when a new tab on my jQueryui tab component is clicked. I have followed several guides and blogs, but can't wrap my head around the process. I have the following:

$('#tabs').tabs({
  select: function(event,ui) {
  alert('selected');
  return false;
  },                
}); 

I don't know what I'm missing, but the alert never fires. I'm am not strong with jQuery so I'm probably making a stupid mistake, so any help will be appreciated.

Thanks, Kris

Update: jsfiddle example http://jsfiddle.net/T7czp/16/

5条回答
唯我独甜
2楼-- · 2019-07-09 09:37

Latest version use

$('#tabs').tabs({
  activate: function(event,ui) {
    alert('selected: '+ui.index);
  }
});
查看更多
放荡不羁爱自由
3楼-- · 2019-07-09 09:39

Are you getting any errors? The trailing , after your select will break the script in some browsers.

This works as seen in this fiddle: http://jsfiddle.net/T7czp/15/

$('#tabs').tabs({
  select: function(event,ui) {
     alert('selected: '+ui.index);
  }
});
查看更多
地球回转人心会变
4楼-- · 2019-07-09 09:50

jquery UI seems to have changed slightly since the last accepted answer.

The ui parameter defined in teh activate function is now an object that looks similar to this:

Object { oldTab={...}, oldPanel={...}, newTab={...}, more...}

The index method is inside each of these. So the updated access method is:

$('#tabs').tabs({
  activate: function(event,ui) {
    alert('selected: '+ui.newTab.index());
  }
});
查看更多
时光不老,我们不散
5楼-- · 2019-07-09 09:52

@Kristofer

You code is correct.

The only thing you must remove in order to the tab get selected is the line:

return false;

because according with the Tabs component documentation:

Note that if a handler for the tabsselect event returns false, the clicked tab will not become selected (useful for example if switching to the next tab requires a form validation).

(the "tabselect" event corresponds to the "select" handler)

查看更多
淡お忘
6楼-- · 2019-07-09 10:01

You need to bind the event to the tab.

$("#tabs").bind("tabsselect", function(e, tab) {
  alert("The tab at index " + tab.index + " was selected");
});
查看更多
登录 后发表回答