简体   繁体   English

面板上的toggleDown仅选中复选框。 Javascript / jQuery

[英]Panel toggleDown only the checkbox checked. Javascript/jQuery

I want to create the slideToggle panel only able to slide down and back after the checkbox checked. 我想创建slideToggle面板,仅在选中该复选框后才能向下和向后滑动。 If the checkbox not checked the slideToggle does not have to work and give me an alert 如果未选中该复选框,则slideToggle不必工作并向我发出警报

The panel can not be opened! 面板无法打开!

But I can not concatenate that. 但是我不能将其串联起来。

 $("#header").click(function(){ $("#panel").slideToggle("slow"); }); $("chckbox").click(function(){ alert("The panel can not be opened!") }); 
 #header, #panel{ padding: 5px; height: 20px; text-align: center; width: 100%; border:1px solid grey; } #panel{ height: 80px; display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <caption for="chckbox">(On/Off)</caption> <input type="checkbox" id="chckbox"> <div id="header">Panel</div> <div id="panel"></div> 

You need to check status of checkbox using checked property on click on #header 您需要点击#header使用checked属性来检查复选框的状态

$("#header").click(function(){
  if ($("#chckbox")[0].checked)
    $("#panel").slideToggle("slow");
  else
    alert("The panel can not be opened!")
});

 $("#header").click(function(){ if ($("#chckbox")[0].checked) $("#panel").slideToggle("slow"); else console.log("The panel can not be opened!") }); 
 #header, #panel{ padding: 5px; height: 20px; text-align: center; width: 100%; border:1px solid grey; } #panel{ height: 80px; display: none; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <caption for="chckbox">(On/Off)</caption> <input type="checkbox" id="chckbox"> <div id="header">Panel</div> <div id="panel"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM