我正在尝试在页面加载时设置当前选项卡(页面重新加载之前查看的最后一个选项卡)。我在stackoverflow上尝试了多种解决方案,但均未成功。
$(function() {
//for bootstrap 3 use 'shown.bs.tab' instead of 'shown' in the next line
$('a[data-toggle="tab"]').on('shown', function (e) {
//save the latest tab; use cookies if you like 'em better:
localStorage.setItem('lastTab', $(e.target).attr('id'));
});
//go to the latest tab, if it exists:
var lastTab = localStorage.getItem('lastTab');
if (lastTab) {
$('#'+lastTab).tab('show');
}
}
);
这是我的标签 HTML
<div class="tabbable"> <!-- Only required for left/right tabs -->
<ul class="nav nav-tabs">
<li><a href="#all" data-toggle="tab">All</a></li>
<li><a href="#this_month" data-toggle="tab">This Month</a></li>
<li><a href="#next_month" data-toggle="tab">Next Month</a></li>
<li><a href="#last_month" data-toggle="tab">Last Month</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="all">
<%= render partial: 'layouts/format_bills', locals: { bill_filter: @all_bills_by_months } %>
</div>
<div class="tab-pane" id="this_month">
<%= render partial: 'layouts/format_bills', locals: { bill_filter: @bills_for_this_month } %>
</div>
<div class="tab-pane" id="next_month">
<%= render partial: 'layouts/format_bills', locals: { bill_filter: @bills_for_next_month } %>
</div>
<div class="tab-pane" id="last_month">
<%= render partial: 'layouts/format_bills', locals: { bill_filter: @bills_for_last_month } %>
</div>
</div>
</div>
(仅针对以下评论中提到的错误)
function setLatestTab() {
var lastTab = localStorage.getItem('lastTab');
if (lastTab) {
$('a[href='+lastTab+']').click();
}
}
setLatestTab();
$('.table #payBills').on('click', function(e) {
setLatestTab();
});
单击链接后不设置最新选项卡。
和支付行动:
def pay
@bill.update_attributes(paid: true)
redirect_to bills_path
end