jquery 判斷 checkbox[] 有沒有checked

想請問一下
我的checkbox是 checkbox[] array
如何用jquery判斷至少要勾一項才能通過?!

阅读 7.1k
4 个回答
if($('input[type="checkbox"]:checked').length > 0)
{
    //code
}

判断选中的checked值,只要有一个是true,循环判断就可以结束,条件为真

$("input[type='checkbox']").is(':checked')

我这渣渣写法

    <label for=""><input type="checkbox" name="" id="" value="1"></label>
    <label for=""><input type="checkbox" name="" id="" value="2"></label>
    <label for=""><input type="checkbox" name="" id="" value="3"></label>

    <button type="button" onclick="pass()">通过?</button>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <script>
        function pass(){
            var $checkbox = $('input[type="checkbox"]');
            console.log($checkbox);
            for(var i=0;i<$checkbox.length;i++){
                console.log(i);
                if($checkbox[i].checked){
                    alert('通过');
                    return;
                }
            }
            if(i==$checkbox.length){
                alert('不通过');
            }
        }
    </script>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题