更改选项卡后禁用 bootstrap 从 link 打开选项卡

Disabling bootstrap open tab from link after tab changing

我一直在努力禁用父 class li 打开带有 link 的选项卡,我只想要保存并继续按钮打开选项卡而不是 links,导致 links 在更改 tab.The 之前的选项卡后被禁用,请问我该如何解决这个问题。只有保存和继续按钮应该移动并返回 TAB

    //the html
    <div class="wizard">
    <div class="wizard-inner">
    <div class="connecting-line"></div>
    <ul class="nav nav-tabs" role="tablist">

    <li role="presentation" class="active" id="step_1"> 
    <a href="#step1" data-toggle="tab" aria-controls="step1" role="tab" title="Step 1">
    <span class="round-tab">
    <div class="numberCircle">1</div>
    </span>
    </a>
    </li>

    <li role="presentation" class="disabled" id="step_2">
    <a href="#step2" data-toggle="tab" aria-controls="step2" role="tab" title="Step 2">
    <span class="round-tab">
    <div class="numberCircle">2</div>
    </span>
    </a>
    </li>
    <li role="presentation" class="disabled" id="step_3">
    <a href="#step3" data-toggle="tab" aria-controls="step3" role="tab" title="Step 3">
    <span class="round-tab">
    <div class="numberCircle">3</div>
    </span>
    </a>
    </li>

    <li role="presentation" class="disabled" id="step_4">
    <a href="#complete" data-toggle="tab" aria-controls="complete" role="tab" title="Complete">
    <span class="round-tab">
    <div class="numberCircle">4</div>
    </span>
    </a>
    </li>
    </ul>
    </div>
    <div class="tab-pane active" role="tabpanel" id="step1"><li><button type="button" class="btn btn-default prev-step" style="margin-top:-30px;">Previous</button></li>
<li><button type="button" class="btn-info-full next-step" style="margin-top:-30px;">Save and continue</button></li></div>


    <div class="tab-pane" role="tabpanel" id="step2"><li><button type="button" class="btn btn-default prev-step" style="margin-top:-30px;">Previous</button></li>
<li><button type="button" class="btn-info-full next-step" style="margin-top:-30px;">Save and Continue</button></li></div>


    <div class="tab-pane" role="tabpanel" id="step3"><li><button type="button" class="prev-step" style="margin-top:-30px;">Previous</button></li>
<li><button type="button" class="btn btn-success btn-info-full next-step" style="margin-top:-30px;">Save and Finish</button></li></div>
    </div>

    </div>
    </div>
    </div>



    //The js tab script           
    <script>
    $(document).ready(function () {
    //Initialize tooltips
    $('.nav-tabs > li a[title]').tooltip();

    //Wizard
    $('a[data-toggle="tab"]').on('show.bs.tab', function (e) {

    var $target = $(e.target);

    if ($target.parent().hasClass('disabled')) {
    return false;
    }
    });

    $(".next-step").click(function (e) {

    var $active = $('.wizard .nav-tabs li.active');
    $active.next().removeClass('disabled');
    nextTab($active);

    var current_tab = e.target; //GOT CURRENT TAB
    var previous_tab = e.relatedTarget; //GOT PREVIOUS TAB

    $previous_tab.parent().addClass('disabled'); //WHAT I TRIED

    });


$(".prev-step").click(function (e) {

var $active = $('.wizard .nav-tabs li.active');
prevTab($active);

});
});

function nextTab(elem) {
$(elem).next().find('a[data-toggle="tab"]').click();
}
function prevTab(elem) {
$(elem).prev().find('a[data-toggle="tab"]').click();
}
</script>

我建议在您的 link 上设置一个自定义点击事件。 data-toggle='tab' 和 role='tab' 表示 Bootstrap 您希望根据这些 link 切换选项卡。因此,我不会在 "Tab shown event" 上注册您的监听器,而是这样做:

$("data-toggle='tab'").on("click", function(e) {
    return $(this).closest("li").hasClass("active");
});

然后,任何单击具有父级 "inactive" 的 link 都不会产生任何效果。

正如@P.Lalonde建议的那样,您可以使用切换选项卡激活和de-activate链接。

$("data-toggle='tab'").on("click", function(e) {
  return $(this).closest("li").hasClass("active");
  });

经过几天的尝试,这是用来解决问题的方法。

    //Updated html
    <div class="wizard">
    <div class="wizard-inner">
    <div class="connecting-line"></div>
    <ul class="nav nav-tabs" role="tablist">

    <li role="presentation" class="active" id="step_1"> 
    <a href="#step1" data-toggle="tab" aria-controls="step1" role="tab" title="Step 1">
    <span class="round-tab">
    <div class="numberCircle">1</div>
    </span>
    </a>
    </li>

    <li role="presentation" class="disabled" id="step_2">
    <a href="#step2" data-toggle="tab" aria-controls="step2" role="tab" title="Step 2">
    <span class="round-tab">
    <div class="numberCircle">2</div>
    </span>
    </a>
    </li>
    <li role="presentation" class="disabled" id="step_3">
    <a href="#step3" data-toggle="tab" aria-controls="step3" role="tab" title="Step 3">
    <span class="round-tab">
    <div class="numberCircle">3</div>
    </span>
    </a>
    </li>

    <li role="presentation" class="disabled" id="step_4">
    <a href="#complete" data-toggle="tab" aria-controls="complete" role="tab" title="Complete">
    <span class="round-tab">
    <div class="numberCircle">4</div>
    </span>
    </a>
    </li>
    </ul>
    </div>
    <div class="tab-pane active" role="tabpanel" id="step1"><li><button type="button" class="btn btn-default prev-step" style="margin-top:-30px;">Previous</button></li>
<li><button type="button" class="btn-info-full next-step" style="margin-top:-30px;">Save and continue</button></li></div>


    <div class="tab-pane" role="tabpanel" id="step2"><li><button type="button" class="btn btn-default prev-step" style="margin-top:-30px;">Previous</button></li>
<li><button type="button" class="btn-info-full next-step" style="margin-top:-30px;">Save and Continue</button></li></div>


    <div class="tab-pane" role="tabpanel" id="step3"><li><button type="button" class="prev-step" style="margin-top:-30px;">Previous</button></li>
<li><button type="button" class="btn btn-success btn-info-full next-step" style="margin-top:-30px;">Save and Finish</button></li></div>
    </div>

    </div>
    </div>
    </div>

       //updated javascript
      <script>
$(document).ready(function () {
//Initialize tooltips
$('.nav-tabs > li a[title]').tooltip();

//Wizard

var activeTab = null;


$('a[data-toggle="tab"]').on('show.bs.tab', function (e) {

activeTab = e.target;

var $target = $(e.target);

if ($target.parent().hasClass('disabled')) {
return false;
}


if($target == "#step1"){
$("#step_2").attr("disabled","disabled");
$("#step_3").attr("disabled","disabled");
$("#step_4").attr("disabled","disabled");
$("#step_2").addClass('disabled');
$("#step_3").addClass('disabled');
$("#step_4").addClass('disabled');
}
else if($target == "#step2"){
$("#step_1").attr("disabled","disabled");
$("#step_3").attr("disabled","disabled");
$("#step_4").attr("disabled","disabled");
$("#step_1").addClass('disabled');
$("#step_3").addClass('disabled');
$("#step_4").addClass('disabled');
} 
else if($target == "#step3"){
$("#step_1").attr("disabled","disabled");
$("#step_2").attr("disabled","disabled");
$("#step_4").attr("disabled","disabled");
$("#step_1").addClass('disabled');
$("#step_2").addClass('disabled');
$("#step_4").addClass('disabled');
} else {
$("#step_1").attr("disabled","disabled");
$("#step_2").attr("disabled","disabled");
$("#step_3").attr("disabled","disabled");
$("#step_1").addClass('disabled');
$("#step_2").addClass('disabled');
$("#step_3").addClass('disabled');  
}

});

$(".next-step").click(function (e) {

var $active = $('.wizard .nav-tabs li.active');
$active.next().removeClass('disabled');
nextTab($active);

});


$(".prev-step").click(function (e) {

var back = $('.wizard .nav-tabs li.previous');
back.next().removeClass('disabled');
prevTab(back);

});

});

function nextTab(elem) {
$(elem).next().find('a[data-toggle="tab"]').click();
}
function prevTab(elem) {
alert(back);    
$(elem).prev().find('a[data-toggle="tab"]').click();
}
</script>