0

enter image description here

As shown in the screenshot, I need the to stay in the question list tab when I submit the form. For the current situation, everytime I refresh or submit the form, it will redirect to the first tab. The current webpage is code in spring boot, thymeleaf, and bootstrap. May I know how to solve it? I will appreciate your guys' helps so much. Thank you!

Below is my nav tab code


<ul class="nav nav-tabs" id="myTab" role="tablist">
    <li class="nav-item" role="presentation">
        <button class="nav-link active" id="home-tab" data-toggle="tab"
            data-target="#new-question" type="button" role="tab"
           aria-controls="home" aria-selected="true">Add New Question
        </button>
    </li>
    <li class="nav-item" role="presentation">
        <button class="nav-link" id="profile-tab" data-toggle="tab"
          data-target="#categorize" type="button" role="tab"
          aria-controls="profile" aria-selected="false">Categorize
       </button>
    </li>
    <li class="nav-item" role="presentation">
        <button class="nav-link" id="contact-tab" data-toggle="tab"
          data-target="#question-list" type="button" role="tab"
           aria-controls="contact" aria-selected="false">Question List
        </button>
    </li>
</ul>

Below is my attempt to use the localstorage

$(document).ready(function(){
    $('a[data-toggle="tab"]').on('show.bs.tab', function(e) {
        localStorage.setItem('activeTab', $(e.target).attr('href'));
    });
    var activeTab = localStorage.getItem('activeTab');
    if(activeTab){
        $('#myTab a[href="' + activeTab + '"]').tab('show');
    }
});
mike kong
  • 47
  • 7
  • 1
    Does this answer your question? [Stop form refreshing page on submit](https://stackoverflow.com/questions/19454310/stop-form-refreshing-page-on-submit) – SuperStormer Mar 29 '22 at 06:06

1 Answers1

1

You can send the form using Ajax, if you do this you can call preventDefault on the event to prevent the default behavior and refresh the page. Like described in the answer of SuperStormer.