简体   繁体   English

如果选中复选框,则显示div?

[英]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);
});

jsFiddle demo jsFiddle演示

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
}

Example

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

相关问题 javascript隐藏/显示div复选框:选中/取消选中 - javascript Hide/show div on checkbox: checked/unchecked 如果选中复选框,如何显示div元素? - How to show div element if checkbox is checked? 如何在div外显示选中的复选框信息 - How to show checked checkbox infomation outside div 如果选中了2个特定的复选框,则显示一个div;如果选中了1个复选框,则显示另一个(如果选中了2个复选框,则不显示) - Show a div if 2 specific checkboxes are checked, show another if 1 checkbox is checked (don't show it if the 2 checkboxes are checked) Javascript - 如果 div 元素(不是复选框元素)上存在“checked”,则显示隐藏的 div - Javascript - show hidden div if 'checked' is present on a div element (not a checkbox element) 对选中复选框时显示/隐藏div的方法进行故障排除 - Troubleshooting a method to show/hide a div when a checkbox is checked jQuery更改类并在选中复选框时显示div - JQuery change class and show div when checkbox is checked 具体怎么做<div>显示是否选中了具有特定名称的复选框? - How to make specific <div> show if checkbox with specific name is checked off? mootools显示/隐藏div取决于是否选中复选框 - mootools show/hide div dependent on whether or not checkbox is checked 选中时使用多个复选框显示/隐藏多个 Div - Show/Hide Multiple Div using multiple checkbox when checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM