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

标签 javascript jquery css html

我有 3 个部分的选项卡部分,每个选项卡都有自己的标题和内容。

但我不想显示所有 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 个复选框,则显示相关的两个选项卡,而第三个选项卡隐藏......等等。

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

最佳答案

您想要实现的目标可以通过重写 showSection 函数轻松实现。每次选择复选框时,您只需切换选项卡的可见性。

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

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

希望这对您有所帮助。

//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>

关于javascript - 如何使用具有相同名称的复选框显示/隐藏选项卡部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53982889/

相关文章:

javascript - (ES6) 无法访问 JSON 数据(未定义错误)

javascript - 我的语法错误,请告知 JavaScript 和 JQuery 的适当语法?

html - CSS伪类

javascript - Bootstrap 设置弹出框内容

javascript - javascript 中的 webkit 转换语法?

javascript - 为什么此 jQuery 代码在 Firefox 中有效,但在 Chrome 或 Safari 中无效?

javascript - Jquery/JS 范围

javascript - 图像加载后触发 JavaScript

jquery - 打字时显示使用 jquery 的东西?

jquery - 使用 jQuery 通过表单数据上传文件时出错