如何使用具有相同名称的复选框显示/隐藏选项卡部分?

时间:2018-12-31 01:49:56

标签: javascript jquery html css html5

我有三个部分的标签部分,每个标签都有自己的标题和内容。

但是我不想显示所有3个选项卡,只是用户通过选中相关复选框选择的内容,有3个相关复选框,每个选项卡一个。

代码如下:

//Function to hide all siblings but leave the clicked one
function hideAllChildrenButOne(parentId, toRevealId) {
	$('#'+parentId).children().css('display', 'none');
	$('#'+toRevealId).css('display', 'block');
}

//Function to show the tab header and content when a checkbox is checked
function showSection(parentId, toRevealId){
	$('#'+parentId).children().css('display', 'none');
	$('#'+toRevealId).css('display', 'block');
	var relatedSection = $('#'+toRevealId).attr('data-section');
	$('#'+relatedSection).css('display', 'block');
}

$(document).ready(function() {

  //On clicking a tab header('Father', 'Mother', 'Brother')
	$('.tab-header').click(function(event) {
		$(this).addClass('tab_active').siblings().removeClass('tab_active');
		var related_section = $(this).attr('data-section');
		hideAllChildrenButOne('relative_content', related_section);
	});

    //On changing any checkbox with name=relative[]
	$("input[name='relative[]']").change( function () {
	    var self = $(this);
	    if (self.is(":checked")) {
	    	showSection('relative_tabs', self.attr('data-header'), self.attr('id'));
	    }
	});
	
});
.relative_container{
    position: relative;
    padding: 45px 15px 15px;
    margin: 0 -15px 15px;
    border-color: #e5e5e5 #eee #eee;
    border-style: solid;
    border-width: 1px 0;
    -webkit-box-shadow: inset 0 3px 6px rgba(0,0,0,.05);
    box-shadow: inset 0 3px 6px rgba(0,0,0,.05);
}
@media (min-width: 768px){
	.relative_container {
	    margin-right: 0;
	    margin-left: 0;
	    background-color: #fff;
	    border-color: #ddd;
	    border-width: 1px;
	    border-radius: 4px 4px 0 0;
	    -webkit-box-shadow: none;
	    box-shadow: none;
	}
}
.relative_tabs{
	margin-bottom: 15px;
	border-bottom: 1px solid #ddd;
	list-style: none;
	padding: 7px 0;
}
.relative_tabs:before{
	display: table;
	content: " ";
}	
.relative_tabs>li{
	display: inline-block;
	margin-bottom: -1px;
}
.relative_tabs>li>a{
	margin-right: 2px;
    line-height: 1.42857143;
    border: 1px solid transparent;
    border-radius: 4px 4px 0 0;
    padding: 9px 15px;
    text-decoration: none;
    cursor: pointer;
}
.relative_tabs>li.tab_active>a{
	color: #555;
    cursor: default;
    background-color: #fff;
    border: 1px solid #ddd;
    border-bottom-color: transparent;
}
.relative_content div{
	display: none;
}
.relative_content>div.active{
	display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form>
	<label>Father<input type="checkbox" name="relative[]" value="Father" data-header="father-tab"></label>
	<label>Mother<input type="checkbox" name="relative[]" value="Mother" data-header="mother-tab"></label>
	<label>Brother<input type="checkbox" name="relative[]" value="Brother" data-header="brother-tab"></label>
	<div class="relative_container">
		<div class="relative_header">
			<ul class="relative_tabs" id="relative_tabs">
				<li id="father-tab" data-section="Father_info" class="tab-header tab_active">
					<a>Father</a>
				</li> 
				<li data-section="Mother_info" class="tab-header" id="mother-tab">
					<a>Mother</a>
				</li>
				<li data-section="Brother_info" class="tab-header" id="brother-tab">
					<a>Brother</a>
				</li>
			</ul>
		</div>
		<div class="relative_content" id="relative_content">
			<div class="tab-content active" id="Father_info">Father Info</div>
			<div class="tab-content" id="Mother_info">Mother Info</div>
			<div class="tab-content" id="Brother_info">Brother Info</div>
		</div>
	</div>
</form>

现在它在选中任何复选框时显示一个选项卡,因此,如果我选中“父亲”复选框,则将显示“父亲”选项卡,然后,如果我选中“母亲”复选框,则将显示“母亲”选项卡,而父亲的选项卡将被隐藏。

我只想显示选中的复选框的选项卡,因此,如果选中1个复选框,则仅显示相关的选项卡,如果选中2个复选框,则显示相关的两个选项卡,第三个选项被隐藏。 >

当然,如果用户选中所有复选框,然后取消选中它们,它们将被隐藏。

1 个答案:

答案 0 :(得分:1)

您可以尝试通过重写showSection函数来轻松实现。每次选择toggle时,您只需tabs就可以看到checkbox的可见性。

function showSection(parentId, toRevealId) {
  $('#' + toRevealId).toggle('display');
  if ($('input[data-header=' + toRevealId + ']').is(':checked')) {
    $('#' + toRevealId).trigger('click');
  }
}

在下面的代码中,除了修改showSection函数外,我最初选择了所有复选框以对齐切换功能。您仍然需要处理代码,以包含在隐藏活动选项卡时选择下一个活动选项卡的功能(例如,如果选中相应的复选框,则上方的if块会选择该选项卡)。

希望这会有所帮助。

//Function to hide all siblings but leave the clicked one
function hideAllChildrenButOne(parentId, toRevealId) {
  $('#' + parentId).children().css('display', 'none');
  $('#' + toRevealId).css('display', 'block');
}

//Function to show the tab header and content when a checkbox is checked
function showSection(parentId, toRevealId) {
  $('#' + toRevealId).toggle('display');
  if ($('input[data-header=' + toRevealId + ']').is(':checked')) {
    $('#' + toRevealId).trigger('click');
  }
}

$(document).ready(function() {

  //On clicking a tab header('Father', 'Mother', 'Brother')
  $('.tab-header').click(function(event) {
    $(this).addClass('tab_active').siblings().removeClass('tab_active');
    var related_section = $(this).attr('data-section');
    hideAllChildrenButOne('relative_content', related_section);
  });

  //On changing any checkbox with name=relative[]
  $("input[name='relative[]']").change(function() {
    var self = $(this);
    //if (self.is(":checked")) {
    showSection('relative_tabs', self.attr('data-header'), self.attr('id'));
    //}
  });

});
.relative_container {
  position: relative;
  padding: 45px 15px 15px;
  margin: 0 -15px 15px;
  border-color: #e5e5e5 #eee #eee;
  border-style: solid;
  border-width: 1px 0;
  -webkit-box-shadow: inset 0 3px 6px rgba(0, 0, 0, .05);
  box-shadow: inset 0 3px 6px rgba(0, 0, 0, .05);
}

@media (min-width: 768px) {
  .relative_container {
    margin-right: 0;
    margin-left: 0;
    background-color: #fff;
    border-color: #ddd;
    border-width: 1px;
    border-radius: 4px 4px 0 0;
    -webkit-box-shadow: none;
    box-shadow: none;
  }
}

.relative_tabs {
  margin-bottom: 15px;
  border-bottom: 1px solid #ddd;
  list-style: none;
  padding: 7px 0;
}

.relative_tabs:before {
  display: table;
  content: " ";
}

.relative_tabs>li {
  display: inline-block;
  margin-bottom: -1px;
}

.relative_tabs>li>a {
  margin-right: 2px;
  line-height: 1.42857143;
  border: 1px solid transparent;
  border-radius: 4px 4px 0 0;
  padding: 9px 15px;
  text-decoration: none;
  cursor: pointer;
}

.relative_tabs>li.tab_active>a {
  color: #555;
  cursor: default;
  background-color: #fff;
  border: 1px solid #ddd;
  border-bottom-color: transparent;
}

.relative_content div {
  display: none;
}

.relative_content>div.active {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form>
  <label>Father<input type="checkbox" name="relative[]" value="Father" data-header="father-tab" checked></label>
  <label>Mother<input type="checkbox" name="relative[]" value="Mother" data-header="mother-tab" checked></label>
  <label>Brother<input type="checkbox" name="relative[]" value="Brother" data-header="brother-tab" checked></label>
  <div class="relative_container">
    <div class="relative_header">
      <ul class="relative_tabs" id="relative_tabs">
        <li id="father-tab" data-section="Father_info" class="tab-header tab_active">
          <a>Father</a>
        </li>
        <li data-section="Mother_info" class="tab-header" id="mother-tab">
          <a>Mother</a>
        </li>
        <li data-section="Brother_info" class="tab-header" id="brother-tab">
          <a>Brother</a>
        </li>
      </ul>
    </div>
    <div class="relative_content" id="relative_content">
      <div class="tab-content active" id="Father_info">Father Info</div>
      <div class="tab-content" id="Mother_info">Mother Info</div>
      <div class="tab-content" id="Brother_info">Brother Info</div>
    </div>
  </div>
</form>