简体   繁体   English

单击div以使用javascript切换其中的复选框

[英]Click on a div to toggle a checkbox inside of it using javascript

This seems to be a simple problem, but I dont use alot of javascript. 这似乎是一个简单的问题,但我不使用很多的JavaScript。

I have a div, with a checkbox in it, and would like the whole div to toggle the checkbox. 我有一个div,其中有一个复选框,并希望整个div切换复选框。 This is what I have so far: 这是我到目前为止:

<div style="padding: 2em; border: 1px solid"
     onClick="if (document.getElementById('cb').checked) document.getElementById('cb').checked=false; else document.getElementById('cb').checked=true;">
  <input name="cb" id="cb" type="checkbox">
</div>

Only problem is when you click the actual checkbox, it get toggled twice, and therefore doesn't change. 唯一的问题是当您单击实际复选框时,它会切换两次,因此不会更改。

Any ideas? 有任何想法吗?

It's possible you could implement this in a more robust and accessible way by using the label element to wrap the area you want to click. 您可以通过使用label元素包装要单击的区域,以更强大和可访问的方式实现此功能。

For example: 例如:

<label for="cb">
    <div style="padding: 2em; border: 1px solid">
        <input name="cb" id="cb" type="checkbox">
    </div>
</label>

I haven't tested the above code, but I believe all browsers support clicking of labels to check an input box. 我没有测试过上面的代码,但我相信所有浏览器都支持点击标签来检查输入框。

onclick="if (event.target.tagName != 'INPUT') document.getElementById('cb').checked = !document.getElementById('cb').checked"

Based on some of the previous answers, this is what worked best for me with html like this: 基于以前的一些答案,这对我来说最适合用这样的html:

<div class="option_item">
  <input type="checkbox" value="1" checked="checked">
</div>

jQuery like this: 像这样的jQuery:

$.fn.toggleCheckbox = function() {
  this.attr('checked', !this.attr('checked'));
}

$(document).ready(function(){
  $(".option_item").click(function (e) {    
    if (e.target.tagName != 'INPUT') {
      $(this).find("input").toggleCheckbox();
      return false;
    }
  });
);

The root of the problem is that when you click on the checkbox, the click event is propagated up the DOM to parent elements. 问题的根源是,当您单击复选框时,单击事件将沿DOM传播到父元素。

So the checkbox handles the click event by toggling itself, and then the DIV receives the click event and toggles the checkbox again. 因此,复选框通过切换自身来处理click事件,然后DIV收到click事件并再次切换复选框。

The simplest solution would be to stop propagation of the event, by adding this to the input element: 最简单的解决方案是通过将此事件添加到input元素来停止事件的传播:

onClick="event.stopPropagation();"

While this is defined in the W3C DOM Level 2 Event Model, it may not be supported in all browsers so you may want to use a cross-browser library like Prototype or jQuery to ensure compatibility. 虽然这是在W3C DOM Level 2事件模型中定义的,但并非所有浏览器都支持它,因此您可能希望使用Prototype或jQuery等跨浏览器库来确保兼容性。

Here is my implementation of checkbox for div 这是我对div的复选框的实现

Link: https://codepen.io/ashwinshenoy/pen/oYXqMV 链接: https//codepen.io/ashwinshenoy/pen/oYXqMV

       <div id="checkboxes">
            <input type="checkbox" name="rGroup" class="check_cat" value="Adventure Sports" id="r1"/>
            <div class="check_overlay">
                <i class="fa fa-check-circle"></i>
            </div>
            <label class="whatever" for="r1">
                <img src="http://i.imgur.com/SfQVTlR.png" class=" img-responsive width100" />
                <div class="row">
                    <div class="col-xs-offset-3 col-xs-3">
                        <div class="check_details">
                            <i class="fa fa-user"><span> 500</span></i>
                        </div><!--check_details end-->
                    </div><!--col-xs-* end-->
                    <div class="col-xs-3">
                        <div class="check_details">
                            <i class="fa fa-bitbucket"><span> 500</span></i>
                        </div><!--check_details end-->
                    </div><!--col-xs-* end-->
                </div><!--inner row end-->
            </label>
            <br>
            <div class="check_name_div">
                Adventure Sports
            </div>
        </div><!--checkboxes end-->

CSS and JS in codepen updated 更新了codepen中的CSS和JS

look into using jQuery rather than programming against the dom yourself. 考虑使用jQuery而不是自己编写dom编程。 using a library like jQuery means your code is more likely to work on most browsers 使用像jQuery这样的库意味着你的代码更有可能在大多数浏览器上运行

http://docs.jquery.com/Effects/toggle http://docs.jquery.com/Effects/toggle

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

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