Как показать / скрыть вкладки разделов, используя флажки с тем же именем? - PullRequest
0 голосов
/ 31 декабря 2018

У меня есть раздел вкладок с 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 Ответ

0 голосов
/ 31 декабря 2018

То, чего вы пытаетесь достичь, можно легко сделать, переписав функцию 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...