jQuery 判断checkbox是否被选中 4种方法

下午写JS验证,有一个需求需要判断 checkbox是否被选择,查阅相关资料后,总结以下4种方法,分享给大家。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery 判断checkbox是否被选中 4种方法</title>
    <script src="jquery-1.8.3.min.js"></script>
    <script>
        $(function(){
            $(‘#agree‘).on(‘click‘, function(){
                if($("input[type=‘checkbox‘]").is(‘:checked‘)){ // 选中返回true,未选中返回false
                    console.log(‘已选中‘);
                }else{
                    console.log(‘未选中‘);
                }
            })

            $(‘#agree2‘).on(‘change‘, function(){ // 可以是 onchange ,也可以是 onclick
                if($(‘#agree2‘).attr(‘checked‘)){
                    console.log(‘已选中‘);
                }else{
                    console.log(‘未选中‘);
                }
            })

            $(‘#agree3‘).on(‘change‘, function(){
                if($("#agree3").get(0).checked){
                    console.log(‘已选中‘);
                }else{
                    console.log(‘未选中‘);
                }
            })

            $(‘#agree4‘).on(‘change‘, function(){
                if($("#agree4").prop(‘checked‘)){
                    console.log(‘已选中‘);
                }else{
                    console.log(‘未选中‘);
                }
            })
        })
    </script>
</head>
<body>
    <input type="checkbox" name="agree" id="agree"> 同意此协议  <br> <br>
    <input type="checkbox" name="agree" id="agree2"> 同意此协议2  <br> <br>
    <input type="checkbox" name="agree" id="agree3"> 同意此协议3  <br> <br>
    <input type="checkbox" name="agree" id="agree4"> 同意此协议4  <br> <br>
</body>
</html>
时间: 2024-08-03 15:18:06

jQuery 判断checkbox是否被选中 4种方法的相关文章

Jquery判断checkbox是否被选中

判断checkbox是否被选中方法一:if ($("#checkbox-id")get(0).checked) { // do something}选中:true没选:false $("#formal").get(0).checkedtrue$("#formal").get(0).checkedfalse 方法二:if($('#checkbox-id').is(':checked')) { // do something}选中:true没选:fa

jquery 判断checkbox 是否选中

这是一个蛋疼的节奏,以前写的代码现在失效了. jquery 判断checkbox 是否被选中,刚开始我是这样写的,而且没问题 $("#ziduana").attr("checked")=="checked" 后来竟然失效了, 后来试了 $("ziduana").attr("checked")==true  , $("#ziduana").is(":checked")

刷新的时候jquery获取checkbox是否为选中和设置选中

$(document).ready(function(){ $('.uninstall_list_checkbox').click(function(){ if($(this).parent('.uninstall_list').children('input').attr("checked")==true){ //判断是否选中 $(this).parent('.uninstall_list').children('input').attr('checked',false); //设置

JQuery 判断checkbox是否选中,checkbox全选,获取checkbox选中值

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

jquery判断单选按钮radio是否选中的方法

JQuery控制radio选中和不选中方法总结 一.设置选中方法 复制代码代码如下: $("input[name='名字']").get(0).checked=true; $("input[name='名字']").attr('checked','true');$("input[name='名字']:eq(0)").attr("checked",'checked'); $("input[name='radio_nam

jquery判断按钮是否被选中了

<script type="text/javascript"> function genjin_view2(elm){ if($(elm).attr("checked")=="checked"){ //jquery判断按钮是否被选中了 alert(888); } } </script>

jQuery属性选择器.attr()和.prop()两种方法

在判断表单单选框是否被选中时,通常会想到使用$('#checkbox').attr('checked')来判断,但在一些情况下,你会发现这种方法并不管用,得到的是undefined. 原来jQuery在1.6版本以后对属性选择器做了一些调整,分为.attr()和.prop()两种方法.为的是区分元素attributes和properties之间模棱两可的东西. 那么它们之间有什么区别呢? 官方的解释是: Attributes vs. Properties The difference betwe

解析Jquery取得iframe中元素的几种方法

DOM方法:父窗口操作IFRAME: ? 1 window.frames["iframeSon"].document IFRAME操作父窗口: ? 1 window.parent.document jquery方法: 在父窗口中操作 选中IFRAME中的所有输入框: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  $(window.frames["iframeSon"].doc

Jquery取得iframe中元素的几种方法(转载)

iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作 DOM方法:父窗口操作IFRAME:window.frames["iframeSon"].documentIFRAME操作父窗口: window.parent.document jquery方法:在父窗口中操作 选中IFRAME中的所有输入框: $(window.frames["iframeSon"].document).find(":text&quo