doushouhe7072 2018-06-20 03:39
浏览 129
已采纳

表单提交后保持相同的Bootstrap选项卡

I have some tabs created using Bootstrap 4 as given in sample snippet:

<ul class="nav nav-pills mb-3" id="pills-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active" id="pills-home-tab" data-toggle="pill" 
            href="#pills-home" role="tab" aria-controls="pills-home" aria- 
            selected="true">Home</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="pills-profile-tab" data-toggle="pill" 
            href="#pills-profile" role="tab" aria-controls="pills-profile" aria- 
            selected="false">Profile</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="pills-contact-tab" data-toggle="pill" 
        href="#pills-contact" role="tab" aria-controls="pills-contact" aria- 
        selected="false">Contact</a>
    </li>
</ul>
<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria- 
        labelledby="pills-home-tab">...</div>
    <div class="tab-pane fade" id="pills-profile" role="tabpanel" aria- 
        labelledby="pills-profile-tab">...</div>
    <div class="tab-pane fade" id="pills-contact" role="tabpanel" aria- 
        labelledby="pills-contact-tab">...</div>
</div>

In one of the tabs, I have a form to be submitted using PHP. However, After the form submission, I am unable to stay in the same tab. How to solve this?

  • 写回答

3条回答 默认 最新

  • doukuai3822 2018-06-20 04:06
    关注

    It is advised that you send form data via AJAX.

    But, coming back to your problem.

    If you are submitting the form via PHP, url will change.

    Add to your form's action:

    <form action="your-php-file.php?tab=pills-contact" method="post">
    

    Now, after form submit, get $_GET['tab']

    $homeTabActive = ! empty($_GET['tab']) && $_GET['tab'] == 'pills-home' ? $_GET['tab'] : 'pills-home'
    

    In your tabs, set active to the respective tab.

    Like this:

    <div class="tab-content" id="pills-tabContent">
        <div class="tab-pane fade show <?php echo $homeTabActive;?>" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">...</div>
    

    Do the same for all tabs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 博通raid 的写入速度很高也很低
  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加