Jquery如何判断checkbox是否选择
                            super                        
                        
                            2023-11-30 17:09                        
                        使用JQuery判断checkbox是否选中:
<html>
<head>
    <meta charset="utf-8">
    <title>使用JQuery判断checkbox是否选中</title>
</head>
<body>
    <input type="checkbox" name="checkbox" />
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">
    // 方式一:使用is(":checked");
    let status = $("input[name='checkbox']").is(":checked");
    console.log(status); // 选中返回true,否则返回false
    // 方式二:使用attr
    let status2 = $("input[name='checkbox']").attr('checked');
    console.log(status2); // 选中返回checked,否则返回undefined
    // 方式三:使用prop
    let status3 = $("input[name='checkbox']").prop('checked');
    console.log(status3); // 选中返回true,否则返回false
</script>
</html>


JS判断checkbox是否选中:
<input type="checkbox" name="checkbox" id="checkboxId" checked />
// 获取checkbox元素
let checkbox = document.getElementById("checkboxId");
// 判断checkbox是否被选中
if (checkbox.checked) {
   console.log("checkbox选中了");
} else {
   console.log("checkbox未选中");
}                0 条讨论 
                                
            