我已經在jsFiddle上工作過,在http://jsfiddle.net/technotarek/ymxn4/。
修訂後的JS,全部內容如下。請注意,小提琴包含了一些新的CSS。我也修改了HTML,但除了#tab-container div之外,它只是爲了外觀。該解決方案與Twitter的引導工作在按他們的文檔推薦的方式在http://twitter.github.com/bootstrap/javascript.html#tabs
var selector;
var selectorID;
activateTab('#myTab a:first');
function activateTab(selectorID)
{
$(selectorID).tab('show')
.closest('.disabled').removeClass('disabled');
}
function deactivateTab(selector)
{
$(selector).off('click.twbstab')
.closest('li').addClass('disabled');
}
$('.btn-demo').on('click',function() {
selector = '#myTab a[href="'+$(this).data('activate')+'"]';
selectorID = $(selector).attr('href');
});
var val1 = $('#frmtype1').validate(
{
errorPlacement: function(error, element) {},
// prevent the standard error message from showing, rather you use the inline-text
rules: {
'Reg_type': {
required: true
}
}
});
// validate 1st form
$('#frmtype1').submit(function(e)
{
// validate the first page
if(val1.form()) {
$('.help-inline').hide();
activateTab(selector);
} else {
$('.help-inline').show();
}
return false;
});
// validate 2nd form
$('#frmtype2').submit(function(e)
{
// validate the second page
activateTab(selector);
return false;
});
// if 2nd or 3rd tab is clicked, validate as if the form was submitted
$('#myTab li:eq(1) a, #myTab li:eq(2) a').click(function(e)
{
selectorID = $(this).attr('href');
// validate the first page
if(val1.form()) {
$('.help-inline').hide();
activateTab(this);
$(selectorID).tab('show');
} else {
$('.help-inline').show();
}
return false;
});
// re-position all tab-panes, except the active pane, so that they are prepared for the slide effect
$(".tab-pane").css("position", "relative");
$(".tab-pane").not(".active").animate({
left: "1000px"
});
// perform slide effect
$('a[data-toggle="tab"]').on('show', function (e) {
lastPane = $(e.relatedTarget).attr('href');
$(lastPane).animate({left: "1000px"}, 300)
currPane = $(e.target).attr('href');
$(currPane).animate({left: "0px"}, 300);
});
來讓一切工作的關鍵是在腳本的末尾(從「//重新定位......」 )。請注意,原始代碼無法工作的原因部分原因是您沒有在演示中使用緩衝庫(根據@四十二評論),但也因爲演示中的代碼用於創建選項卡效果與標籤在Twitter Bootstrap中的工作方式根本不同。
您鏈接到的演示使用jQuery緩動插件。我沒有使用它自己,但這是一個鏈接:http://gsgd.co.uk/sandbox/jquery/easing/ –