6
5

More than 5 years have passed since last update.

checkbox(チェックボックス)のチェック

Posted at

解説

枠のクリックイベントを取得して、checkboxを制御する。
checkboxをクリックした場合も枠のイベントが発生する為、イベント元で処理を切り分ける。

ソース

DEMO

<!DOCTYPE html>
<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
        <meta charset="utf-8">
        <title>CheckBox</title>
    </head>
    <style>
    .box {
        width: 100px;
        height: 100px;
        border:1px black solid;
    }
    </style>
    <script>
    $(function(){
        $(".box").click(function(e){
            if(e.target==this){
                var $i = $(this).find("input");
                $i.prop("checked", !$i.prop("checked"));
                $i.trigger("change");
            }
        });
    });
    </script>
    <body>
        <div class="box">
            <input type="checkbox">
        </div>
    </body>
</html>
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5