禁用特定选项卡的按钮

时间:2017-03-01 08:07:04

标签: javascript jquery html5

我有两个标签: -

<div class="modal-body">
   <form name="myForm" novalidate="novalidate">
    <ul class="nav nav-tabs">
       <li class="active"><a data-toggle="tab" href="#basicInfo">Info</a></li>
       <li><a data-toggle="tab" href="#view">View Details</a></li>
    </ul>

  <div class="tab-content">
     <div id="basicInfo"  class="tab-pane fade in active modal-table-div">
      .....
     </div>
  <div id="view" class="tab-pane modal-table-div fade">
     .....
     </div>
</div>

删除并添加按钮按钮:

<div class="modal-footer">
     <button type="button" class="btn btn-danger">Delete</button>
     <button type="button" class="btn btn-success">Add</button>
</div>

我想要的是当我点击“查看详细信息”标签时,删除按钮和添加按钮被禁用,反之亦然。我应该如何实现?

3 个答案:

答案 0 :(得分:4)

我会向diableButton添加一个类link,它将触发禁用功能,使用下面的代码。

此行$(this).find("a").hasClass("diableButton")会询问我们的link是否有该课程。

&#13;
&#13;
$('.nav-tabs li').click(function() {
  if ($(this).find("a").hasClass("diableButton")) {
    $('.modal-footer button').prop("disabled", true);
  } else {
    $('.modal-footer button').prop("disabled", false);
  }
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="modal-body">
  <form name="myForm" novalidate="novalidate">
    <ul class="nav nav-tabs">
      <li class="active"><a data-toggle="tab" href="#basicInfo">Info</a></li>
      <li><a data-toggle="tab" class="diableButton" href="#view">View Details</a></li>
    </ul>

    <div class="tab-content">
      <div id="basicInfo" class="tab-pane fade in active modal-table-div">
        .....
      </div>
      <div id="view" class="tab-pane modal-table-div fade">
        .....
      </div>
    </div>

    <div class="modal-footer">
      <button type="button" class="btn btn-danger">Delete</button>
      <button type="button" class="btn btn-success">Add</button>
    </div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
  var href= $(e.target).attr("href");
  // check href here and do what you want
});

并检查Bootstrap JS Tab所有活动

答案 2 :(得分:1)

您可以使用.prop()方法禁用按钮:

var isViewTab = $('.modal-body li.active a').attr('href') === "#view";
$(".modal-body").find(".btn-danger,.btn-success") // get the modal buttons
                .prop('disabled', isViewTab ) // disabled them
                .toggleClass('disabled', isViewTab); // add a class to style them disbled