The Togglable tabs plugin has been intend to use on a single page. Clicking a tab triggers a navigation to a hash tag such as #tab2
, hash tag links don't reload the page the javascript plugin catch the click and sets the tab active base on the hash tag.
In your code the forms have a action set to a page (weathercomment.html
) and not an anchor. So submitting the form reloads the page (and set the tab that have got hard code class="active"
as the active tab again).
When you indeed want to load a different page (url) after submitting you should code the class="active"
dynamically based on the request_uri
.
Alternatively you could return false
on the submit event of the form, which don't reload your page. (see also: How to prevent buttons from submitting forms). Before the return false
you could use the form input values to update your database or do something else. You can do a ajax call here too.
Example:
html
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#home" role="tab" data-toggle="tab">Home</a></li>
<li role="presentation"><a href="#profile" role="tab" data-toggle="tab">Profile</a></li>
<li role="presentation"><a href="#messages" role="tab" data-toggle="tab">Messages</a></li>
<li role="presentation"><a href="#settings" role="tab" data-toggle="tab">Settings</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">...</div>
<div role="tabpanel" class="tab-pane" id="profile">
<div id="formresponse"></div>
<form role="form" id="profileform">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
<div role="tabpanel" class="tab-pane" id="messages">...</div>
<div role="tabpanel" class="tab-pane" id="settings">...</div>
</div>
javascript
$('#profileform').on('submit', function() {
$('#formresponse').html('Your email:' + $('#exampleInputEmail1').val());
return false;
});
demo: http://www.bootply.com/1GqfQaJLoQ