li标签和checkbox绑定

参考原文:https://www.cnblogs.com/youxin/p/3885496.html

我们经常需要li或span包含一个checkbox,不管点击checkbox或li都会触发相应的事件,如背景色变色。

<ul>
    <li><input type="checkbox" name="" id=""/>li1</li>
    <li><input type="checkbox" name="" id=""/>li2</li>
    <li><input type="checkbox" name="" id=""/>li3</li>
    <li><input type="checkbox" name="" id=""/>li4</li>
</ul>
$("ul li").click(function(){
        var $input=$(this).find("input");
        if($input.prop("checked"))
        {
            $input.prop("checked",false);
            $(this).css("background-color","");
        }
        else
        {
            $input.prop("checked",true);
            $(this).css("background","red");
        }
    });
原文地址:https://www.cnblogs.com/zhangyouwu/p/9259308.html