【问题标题】:Bootstrap nav-tabs check for selected tab using jqueryBootstrap nav-tabs 使用 jquery 检查选​​定的选项卡
【发布时间】:2015-11-17 06:59:01
【问题描述】:

我有以下引导导航选项卡:

 <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>

当我点击标签时它可以工作,但警报总是显示延迟spiff。当他们单击 Instantspiff 选项卡时,我需要显示 Instantspiff。谁能看到我做错了什么?

【问题讨论】:

    标签: javascript jquery twitter-bootstrap


    【解决方案1】:

    您只需从所有选项卡中删除类 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>
    

    更新小提琴:https://jsfiddle.net/DTcHh/14313/

    【讨论】:

    • DinoMyte 我试过这个,我添加了我的条件语句,但无论我选择哪个选项卡,它仍然总是在警报框中显示delayedspiff。我的 if 语句有问题吗?
    • 它不会为您提供所选选项卡的 id,因为该事件尚未完成单击新选项卡。我已经更新了解决方案。希望这可以帮助您实现所需的目标。
    • 等我明白了!感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多