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

标签 javascript jquery twitter-bootstrap

我有以下 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/

相关文章:

javascript - 如何使用 jQuery 更改 Bootstrap 按钮样式

javascript - 为什么需要复制数组才能对其使用方法?

javascript - jQuery加载字符编码问题

javascript - 使用数组中的颜色

jquery - 想要使用 Jquery 或 javascript 在不点击的情况下调用 rel 属性

javascript - 使用 Twitter Bootstrap(使用 jQuery)输入货币

javascript - Bootstrap col-md 问题

css - 为比 div 宽的表格启用水平滚动条,而不指定固定宽度

javascript - 使用 remove() 从列表中删除某些项目后显示相同的长度

javascript - 控制台不会记录 div 数组