如何以编程方式切换到新的 jQuery UI 选项卡?
我正在构建一个类似于 jQuery UI 选项卡的“签出”交互。这意味着单击第一个选项卡上的按钮将停用第一个选项卡并移至下一个选项卡。我一直在梳理 Stack Overflow 上的帖子,但我尝试的任何方法似乎都不起作用。我使用的是 jQuery UI 1.8,代码如下:
$(document).ready(function() {
var $tabs = $('#tabs').tabs({ selected: 0 });
$tabs.tabs('option', 'selected', 0);
$("#tabs").tabs({disabled: [1,2]});
$("#addstudent").click(function(){
$tabs.tabs('option', 'selected', 1);
$("#tabs").tabs({disabled: [0,2]});
});
$("#confirm").click(function(){
$tabs.tabs('option', 'selected', 2);
$("#tabs").tabs({disabled: [0,1]});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.8.24/jquery-ui.min.js"></script>
<div id="tabs">
<ul>
<li><a href="#fragment-1">Student Information</a></li>
<li><a href="#fragment-2">Confirmation</a></li>
<li><a href="#fragment-3">Invoice</a></li>
</ul>
<div id="fragment-1">
<button id="addstudent" >Add Student</button>
</div>
<div id="fragment-2">
<button id="confirm" >Confirm</button>
</div>
<div id="fragment-3">
tab 3, index 2
</div>
</div>
当我单击按钮时,下一个选项卡将被解锁(因为它是可选的),但它不会禁用索引 0 处的选项卡并切换到索引 1 处的选项卡。此外,相应的面板不会显示。
I am building a "check-out" like interaction with jQuery UI tabs. This means that the click of a button on the first tab will deactivate the first tab and move to the next. I have been combing through the posts on Stack Overflow but nothing I try seems to work. I am using jQuery UI 1.8, here is the code:
$(document).ready(function() {
var $tabs = $('#tabs').tabs({ selected: 0 });
$tabs.tabs('option', 'selected', 0);
$("#tabs").tabs({disabled: [1,2]});
$("#addstudent").click(function(){
$tabs.tabs('option', 'selected', 1);
$("#tabs").tabs({disabled: [0,2]});
});
$("#confirm").click(function(){
$tabs.tabs('option', 'selected', 2);
$("#tabs").tabs({disabled: [0,1]});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.8.24/jquery-ui.min.js"></script>
<div id="tabs">
<ul>
<li><a href="#fragment-1">Student Information</a></li>
<li><a href="#fragment-2">Confirmation</a></li>
<li><a href="#fragment-3">Invoice</a></li>
</ul>
<div id="fragment-1">
<button id="addstudent" >Add Student</button>
</div>
<div id="fragment-2">
<button id="confirm" >Confirm</button>
</div>
<div id="fragment-3">
tab 3, index 2
</div>
</div>
When I click the buttons, the next tab becomes unlocked (in that it is selectable) but it does not disable the tab at index 0 and switch to the tab at index 1. Also, the corresponding panel does not show.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于 jQuery UI 1.9+,select 方法已被弃用 API。在 1.9+ 中,您需要使用
option
和active
。从文档中:
For jQuery UI 1.9+, the select method has been deprecated in the API. In 1.9+, you need to use
option
andactive
, instead.From the documentation:
尝试将代码更改为:
JSBin 示例
Try changing your code to this:
JSBin Example
@Aaron Sherman 已经回答了你的问题。这是详细步骤。
这是代码的 JS 部分:
以下是要添加到选项卡 div 中的“a href”标签
示例:
@Aaron Sherman already answered your question. Here is the detailed step.
Here is JS part of the code:
Here are the "a href" tags to be added in your tabs div
Example:
我修改了 @Mahesh Vemuri 的解决方案,添加了禁用第一个步骤之后的可能性,然后在单击“下一步”按钮后启用它们:
JScode:
HTML 是相同的。
PS:请注意,使用此代码,单击“NEXT”按钮将启用下一个选项卡(之前已禁用),但单击“PREV”按钮将不会禁用当前选项卡,以便允许用户向后和向前进入顺序流程,直到下一个禁用选项卡.
希望,如果选项卡包含表单的 3 个步骤,则只有 JS 表单验证 成功时才能触发 NEXT 按钮的操作。
I modified solution of @Mahesh Vemuri adding possibility to disable steps following the first and then enable them after clicking "NEXT" button:
JScode:
The HTML is the same.
P.S.: Note that with this code, clicking NEXT button will enable next tab (previously disabled) but clicking PREV button will not disable current tab, in order to allow user to go backward and forward into a sequential flow until next disabled tab.
Hopefully, if Tabs contain 3 steps of a form, action of NEXT button could be fired only if a JS Form Validation will have success.