[英]if checkbox is checked show div?
i have a checkbox like this: 我有一个这样的复选框:
<input type="checkbox" id="terms" name="terms" value="yes" class="tick2">
and i am trying to use jquery to try and display div id '#content_more' when the checkbox is ticked and if it is unticked to re-hide the div. 我正在尝试使用jquery尝试在选中复选框并且未选中以重新隐藏div时显示div id'#content_more'。
i am using this and i am not able to get it to work, can anyone please show me where i am going wrong, thanks 我正在使用它,但我无法使它正常工作,有人可以告诉我我要去哪里了,谢谢
<script>
$('.tick2').click(function() {
if( $(this).is(':checked')) {
$("#content_more").show();
}
});
</script>
html: HTML:
<div class="content_more">hello</div>
css: CSS:
#content_more{
width: 990px;
margin: auto;
background: #ddd;
height: auto;
overflow: hidden;
margin-top:50px;
font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:14px;
background-color: white;
padding: 9px 11px;
background: rgba(255, 255, 255, 0.9);
border: 1px solid #c5c5c5;
-webkit-box-shadow: 0 3px 8px rgba(0, 0, 0, .25);
-moz-box-shadow: 0 3px 8px rgba(0, 0, 0, .25);
box-shadow: 0 3px 8px rgba(0, 0, 0, .25);
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
margin-bottom:60px;
dipslay:none;
Depending on the structure of your HTML, you can accomplish this without a single line of JavaScript using the :checked
CSS pseudo selector - given your CSS and content, here are a few examples: 根据HTML的结构,您可以使用:checked
CSS伪选择器而无需一行JavaScript来完成此操作-给定您的CSS和内容,以下是一些示例:
HTML Scenario 1 - immediately adjacent element: HTML场景1-紧邻元素:
<input type="checkbox" id="terms" />
<div class="content_more">hello</div>
CSS solution: CSS解决方案:
#terms:checked + .content_more {
display: block;
}
HTML Scenario 2 - non-adjacent sibling: HTML方案2-不相邻的同级:
<input type="checkbox" id="terms" />
<div>foo</div>
<div>bar</div>
<div class="content_more">hello</div>
CSS solution: CSS解决方案:
#terms:checked ~ .content_more {
display: block;
}
HTML Scenario 3 - target nested within non-adjacent sibling: HTML方案3-目标嵌套在非相邻的同级中:
<input type="checkbox" id="terms" />
<div>foo</div>
<div>bar</div>
<section>
<div class="content_more">hello</div>
</section>
CSS solution: CSS解决方案:
#terms:checked ~ section .content_more {
display: block;
}
This solution depends on your HTML structure - if you provide a bit more in your question, I could be more specific with the CSS examples above. 此解决方案取决于您的HTML结构-如果您在问题中提供的内容更多,我可以在上面的CSS示例中更加具体。
Use the change
event (to recognise non-mouse interactions such as the user using their keyboard), and then use the .toggle()
function: 使用change
事件(以识别非鼠标交互,例如用户使用他们的键盘),然后使用.toggle()
函数:
$('.tick2').change(function(e) {
$('#content_more').toggle(this.checked);
});
Note that if your <script>
tag comes before the actual HTML to create the elements then the jQuery will execute before there are any elements to select, and therefore not bind any event handlers. 请注意,如果您的<script>
标记位于创建元素的实际HTML之前,则jQuery将在有任何元素可供选择之前执行,因此不会绑定任何事件处理程序。 To resolve that you'll need to use a DOM ready handler: 要解决此问题,您需要使用DOM ready处理程序:
$(document).ready(function () {
$('.tick2').change(function (e) {
$('#content_more').toggle(this.checked);
});
});
Try This: 尝试这个:
$(document).ready(function(){
$('.tick2').click(function() {
$(".content_more").toggle("hide");
});
});
Working fiddle: http://jsfiddle.net/robertrozas/x5r98/ 工作提琴: http : //jsfiddle.net/robertrozas/x5r98/
You need to change to class in your jQuery selector and fix the display typo in your CSS. 您需要在jQuery选择器中更改类,并在CSS中修复显示错误。
Try this: 尝试这个:
$(document).ready(function () {
$('.tick2').click(function () {
$(".content_more").toggle(!this.checked);
});
});
CSS CSS
// ...
border-radius: 3px;
margin-bottom:60px;
display:none; // typo here in your code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.