jQuery checkbox 操作

  • 判断是否选中 && 获取选中的value

    <input type="checkbox" name="sel" class="sel" value="1">
    <input type="checkbox" name="sel" class="sel" value="2">
    selArr = new Array()
    $('.sel').each(function () {
        // 如果已选中
        if($(this).is(':checked')){
            selArr.push($(this).val());
        }
    });

     

    方法一:
    if ($("#checkbox-id").get(0).checked) {
        // do something
    }
    
    方法二:
    if($('#checkbox-id').is(':checked')) {
        // do something
    }
    
    方法三:
    if ($('#checkbox-id').attr('checked')) {
        // do something
    }

    判断复选框是否被选中

    在jQuery1.6版本之后,取复选框有没有被选中,要用prop

    $('#checkbox-id').prop('checked')//返回值是true或者false

    设置复选框是否为选中

    $("input[name='selectit']").prop("checked",true/false)
    
    $("input[name='selectit']").prop("checked",$("#selectit").prop('checked'))
    
    {
    
      //do something
    } 
    function checkInfo(){ $("input[name='org3.otherValues']").each( function(){ if($(this).get(0).checked){ returntrue; } });

    var org3_ids=$("#org3_ids").val(); if(org3_ids!=''){ return true; } alertMsg.warn("请选择接收人!");return false; } 获取选择 radio 的值 $("input:radio[name='checkboxName']:checked").val()

     

     

posted @ 2019-12-24 23:16  GJH-  阅读(69)  评论(0)    收藏  举报