gpt4 book ai didi

javascript - Bootstrap nav-tabs 使用 jquery 检查所选选项卡

转载 作者:行者123 更新时间:2023-12-03 08:21:06 26 4
gpt4 key购买 nike

我有以下 Bootstrap 导航选项卡:

 <div class="row spiff_tabs_body">
<!-- Nav tabs -->
<ul class="nav nav-tabs spiff_tabs" role="tablist">
<li role="presentation" class="active">
<a href="#home" aria-controls="home" role="tab" data-toggle="tab" onclick="FormGet('dashboard/delayedspiff', 'delayedspiff')">Potential Spiff</a>
</li>
<li role="presentation">
<a href="#profile" aria-controls="profile" role="tab" data-toggle="tab" onclick="FormGet('dashboard/instantspiff', 'delayedspiff')">Instant Spiff</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="delayedspiff"></div>
<div role="tabpanel" class="tab-pane" id="instantspiff"></div>
</div>
</div>
</div>
</div>

我需要能够检查选择了哪个选项卡,然后显示警报。我认为有以下 JavaScript:

<script>
$(function () {
FormGet('dashboard/delayedspiff', 'delayedspiff');
});
</script>

<script>
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
// here is the new selected tab id
var selectedTabId = e.target.id;
var id = $('.tab-content .active').attr('id');
if (id == "delayedspiff") {
alert("delayedspiff");
} else {
alert("instantspiff");
}
});
</script>

当我单击选项卡时,它可以工作,但警报始终显示delayspiff。当他们单击 instantspiff 选项卡时,我需要显示 instantspiff。谁能看出我做错了什么吗?

最佳答案

您只需从所有选项卡中删除类 active 并将其添加到被单击的选项卡中。

编辑:为了获取单击的选项卡的 ID,请尝试在选项卡选择上使用属性 data-id。

  <li role="presentation" class="active">
<a href="#home" aria-controls="home" role="tab" data-id="delayedspiff" data-toggle="tab" onclick="FormGet('dashboard/delayedspiff', 'delayedspiff')">Potential Spiff</a>
</li>
<li role="presentation">
<a href="#profile" aria-controls="profile" data-id="instantspiff" role="tab" data-toggle="tab" onclick="FormGet('dashboard/instantspiff', 'delayedspiff')">Instant Spiff</a>
</li>

<script>
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
var wrongid = $('.tab-content .active').attr('id');
$('a[data-toggle="tab"]').removeClass("active"); // remove class active from all tabs
$(this).addClass("active"); // add class active to the current tab
var correctid = $(this).data("id"); // get the attribute data-id of the clicked tab
alert($('.tab-content .active')[0].outerHTML); // shows why you are getting the incorrect id
if (correctid == "delayedspiff")
alert("delayedspiff");
else
alert("instantspiff");
});
</script>

更新了 fiddle :https://jsfiddle.net/DTcHh/14313/

关于javascript - Bootstrap nav-tabs 使用 jquery 检查所选选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33745637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com