0

您如何为 jquery UI 选项卡的每个索引绑定一个函数?

例如,我正在创建一个由 3 部分组成的幻灯片注册,第 1 步是一个表单并且具有验证功能,我想将其代码放在第 1 步的负载中,同时还向选项卡添加类以禁用 #2 和 #3当打开 1 时,禁用 #1 和 #3 当打开 #2

4

1 回答 1

4

无需将功能绑定到选项卡,它内置在插件中:

插件页面

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

在函数内部,您可以确定您所在的当前选项卡并从那里执行您需要执行的操作:

  • 获取当前标签索引

    currentTabIndex = $('#tabs').tabs('option', 'selected')
    
  • 获取当前选项卡内容 ID(来自 href) - 可能有更简单的方法,但我还没有找到。

    currentTabContent = $( $('.ui-tabs-selected').find('a').attr('href') );
    

但是从看到你发布的关于你试图使用的这个选项卡/表单系统的其他问题,我在这里拼凑了一个演示

HTML

<div id="tabs">
 <ul class="nav"> <!-- this part is used to create the tabs for each div using jquery -->
  <li class="ui-tabs-selected"><a href="#part-1"><span>One</span></a></li>
  <li><a href="#part-2"><span>Two</span></a></li>
  <li><a href="#part-3"><span>Three</span></a></li>
 </ul>

 <div id="part-1">
  <form name="myForm" method="post" action="" id="myForm">
   <div class="error"></div>
    Part 1
    <br /><input type="checkbox" /> #1 Check me!
    <br />
    <br /><input id="submitForm" type="button" disabled="disabled" value="next >>" />
  </form>
 </div>

 <div id="part-2">
  <div class="error"></div>
   Part 2
   <br />Search <input type="text" />
   <br />
   <br /><input id="donePart2" type="button" value="next >>" />
 </div>

 <div id="part-3">
  <div class="error"></div>
   Part 3:
   <br />Some other info here
 </div>
</div>

脚本

$(document).ready(function(){
 // enable Next button when form validates
 $('#myForm').change(function(){
  if (validate()) {
   $('#submitForm').attr('disabled','')
  } else {
   $('#submitForm').attr('disabled','disabled')
  }
 })
 // enable form next button
 $('#submitForm').click(function(){
    // enable the disabled tab before you can switch to it, switch, then disable the others.
  if (validate()) nxtTab(1,2);
 })
 // enable part2 next button
 $('#donePart2').click(function(){
    var okForNext = true; // do whatever checks, return true 
  if (okForNext)  nxtTab(2,1);
 })
 // Enable tabs
 $('#tabs').tabs({ disabled: [1,2], 'selected' : 0 });
})
function validate(){
 if ($('#myForm').find(':checkbox').is(':checked')) return true;
 return false;
}
function nxtTab(n,o){
 // n = next tab, o = other disabled tab (this only works for 3 total tabs)
 $('#tabs').data('disabled.tabs',[]).tabs( 'select',n ).data('disabled.tabs',[0,o]);
}
于 2009-12-15T21:32:08.077 回答