根据滑块(复选框)选择显示/隐藏div

时间:2018-06-11 15:36:37

标签: html css

有没有办法根据滑块的显示/隐藏hiddencar div?我不知道如何使用:目标选择器,我相信它可以单独用css完成

是显示div没有隐藏div



	    .onoffswitch4 {
        position: relative; width: 101px;
        -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
    }
    .onoffswitch-checkbox4   {
        display: none;
    }
    .onoffswitch-label4 {
        display: block; overflow: hidden; cursor: pointer;
        border: 2px solid ; border-radius: 11px;
    }
    .onoffswitch-inner4 {
        display: block; width: 200%; margin-left: -100%;
        transition: margin 0.3s ease-in 0s;
    }
    .onoffswitch-inner4:before, .onoffswitch-inner4:after {
        display: block; float: left; width: 50%; height: 38px; padding: 0; line-height: 38px;
        font-size: 13px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
        box-sizing: border-box;
    }
    .onoffswitch-inner4:before {
        content: "No";
        padding-left: 14px;
        background-color: #34A7C1; color: #FFFFFF;
    }
    .onoffswitch-inner4:after {
        content: "Yes";
        padding-right: 14px;
        background-color: #EEEEEE; color: #999999;
        text-align: right;
    }
    .onoffswitch-switch4 {
        display: block; width: 9px; margin: 14.5px;
        background: #FFFFFF;
        position: absolute; top: 0; bottom: 0;
        right: 59px;
        border: 2px solid ; border-radius: 11px;
        transition: all 0.3s ease-in 0s; 
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-inner4 {
        margin-left: 0;
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-switch4 {
        right: 0px; 
    }

   <div id="contact_phone">Do you have a car?: *<br>
     <div class="onoffswitch4">
        <input type="checkbox" name="onoffswitch4" class="onoffswitch-checkbox4" id="myonoffswitch4" checked>
        <label class="onoffswitch-label4" for="myonoffswitch4">
            <span class="onoffswitch-inner4"></span>
            <span class="onoffswitch-switch4"></span>
        </label>
    </div>
    
    
<div id="hiddencar">							
				
	<div id="contact_name">Year: *<br>
	<input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
    </div> 
	
        
	<div id="contact_phone">Make: *<br>
	<input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
	</div>    
      
      
     <div id="contact_phone">Model: *<br>
     <input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
    </div>   
    	
</div><!--hiddencar-->
    
    
    
   </div><!--contact_phone-->
&#13;
&#13;
&#13;

有没有办法根据滑块的显示/隐藏hiddencar div?我不知道如何使用:目标选择器,我相信它可以单独用css完成

是显示div没有隐藏div

3 个答案:

答案 0 :(得分:0)

您将不得不听取复选框,然后检查其值以隐藏或显示隐藏的汽车div。

首先抓住Dom元素。

其次,向复选框添加一个事件监听器,每当有人更改复选框值时,它将返回一个布尔(true或false)语句,并从那里你可以切换一个隐藏类

const checkbox = document.querySelector('input[name=onoffswitch4]')
const hiddenCar = document.getElementById('hiddencar')

checkbox.addEventListener('change', (e) => {
  if (!e.target.checked) {
    hiddenCar.classList.add('hide')
  } else {
    hiddenCar.classList.remove('hide')
  }
})

<强> CSS

#hiddencar {
  display: block;
}

#hiddencar.hide {
  display: none;
}

Codepen https://codepen.io/anon/pen/yEMEap?editors=0001

答案 1 :(得分:0)

出于某种原因,CSS滑块实际上并没有改变复选框的checked属性,我在我的JS解决方案中添加了这个,以及showHideCarFields函数,它将根据需要显示或隐藏汽车字段

&#13;
&#13;
$(document).ready(function() {
  $(document).on("click", ".onoffswitch4", function() {
    $("#myonoffswitch4").prop("checked", !$("#myonoffswitch4").prop("checked"))
    showHideCarFields();
  });
  showHideCarFields();
});

function showHideCarFields() {
    var isChecked = $('#myonoffswitch4').prop("checked");
    if(isChecked) {
      $('#hiddencar').hide();
    } else {
      $('#hiddencar').show();
    }
}
&#13;
.onoffswitch4 {
        position: relative; width: 101px;
        -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
    }
    .onoffswitch-checkbox4   {
        display: none;
    }
    .onoffswitch-label4 {
        display: block; overflow: hidden; cursor: pointer;
        border: 2px solid ; border-radius: 11px;
    }
    .onoffswitch-inner4 {
        display: block; width: 200%; margin-left: -100%;
        transition: margin 0.3s ease-in 0s;
    }
    .onoffswitch-inner4:before, .onoffswitch-inner4:after {
        display: block; float: left; width: 50%; height: 38px; padding: 0; line-height: 38px;
        font-size: 13px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
        box-sizing: border-box;
    }
    .onoffswitch-inner4:before {
        content: "No";
        padding-left: 14px;
        background-color: #34A7C1; color: #FFFFFF;
    }
    .onoffswitch-inner4:after {
        content: "Yes";
        padding-right: 14px;
        background-color: #EEEEEE; color: #999999;
        text-align: right;
    }
    .onoffswitch-switch4 {
        display: block; width: 9px; margin: 14.5px;
        background: #FFFFFF;
        position: absolute; top: 0; bottom: 0;
        right: 59px;
        border: 2px solid ; border-radius: 11px;
        transition: all 0.3s ease-in 0s; 
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-inner4 {
        margin-left: 0;
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-switch4 {
        right: 0px; 
    }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contact_phone">Do you have a car?: *<br>
     <div class="onoffswitch4">
        <input type="checkbox" name="onoffswitch4" class="onoffswitch-checkbox4" id="myonoffswitch4" checked>
        <label class="onoffswitch-label4" for="myonoffswitch4">
            <span class="onoffswitch-inner4"></span>
            <span class="onoffswitch-switch4"></span>
        </label>
    </div>
    
    
<div id="hiddencar">							
				
	<div id="contact_name">Year: *<br>
	<input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
    </div> 
	
        
	<div id="contact_phone">Make: *<br>
	<input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
	</div>    
      
      
     <div id="contact_phone">Model: *<br>
     <input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
    </div>   
    	
</div><!--hiddencar-->
    
    
    
   </div><!--contact_phone-->
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以将hiddencar元素移动到复选框标签后面,然后使用多个+选择器选择标签后面的元素,如下所示:

&#13;
&#13;
    .onoffswitch4 {
         position: relative; width: 101px;
        -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
    }
    .onoffswitch-checkbox4   {
        display: none;
    }
    .onoffswitch-label4 {
        display: block; overflow: hidden; cursor: pointer;
        border: 2px solid ; border-radius: 11px;
    }
    .onoffswitch-inner4 {
        display: block; width: 200%; margin-left: -100%;
        transition: margin 0.3s ease-in 0s;
    }
    .onoffswitch-inner4:before, .onoffswitch-inner4:after {
        display: block; float: left; width: 50%; height: 38px; padding: 0; line-height: 38px;
        font-size: 13px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
        box-sizing: border-box;
    }
    .onoffswitch-inner4:before {
        content: "No";
        padding-left: 14px;
        background-color: #34A7C1; color: #FFFFFF;
    }
    .onoffswitch-inner4:after {
        content: "Yes";
        padding-right: 14px;
        background-color: #EEEEEE; color: #999999;
        text-align: right;
    }
    .onoffswitch-switch4 {
        display: block; width: 9px; height: 9px; margin: 14.5px;
        background: #FFFFFF;
        position: absolute; top: 0; bottom: 0;
        right: 59px;
        border: 2px solid ; border-radius: 11px;
        transition: all 0.3s ease-in 0s; 
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-inner4 {
        margin-left: 0;
    }
    .onoffswitch-checkbox4:checked + .onoffswitch-label4 .onoffswitch-switch4 {
        right: 0px; 
    }

    .onoffswitch-checkbox4:checked + .onoffswitch-label4 + #hiddencar {
        display: none;
    }
&#13;
   <div id="contact_phone">Do you have a car?: *<br>
     <div class="onoffswitch4">
        <input type="checkbox" name="onoffswitch4" class="onoffswitch-checkbox4" id="myonoffswitch4" checked>
        <label class="onoffswitch-label4" for="myonoffswitch4">
            <span class="onoffswitch-inner4"></span>
            <span class="onoffswitch-switch4"></span>
        </label>
        
       <div id="hiddencar">								
	       <div id="contact_name">Year: *<br>
	           <input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
           </div> 
	
        
	       <div id="contact_phone">Make: *<br>
	           <input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
	       </div>    
      
      
            <div id="contact_phone">Model: *<br>
                <input id="element_4" name="name" class="element text medium" type="text" maxlength="50" value="" placeholder="FULL NAME"/>
            </div>   
    	
        </div><!--hiddencar-->
    </div>

   </div><!--contact_phone-->
&#13;
&#13;
&#13;