禁用和检查已检查和禁用的支柱为什么返回false?

时间:2015-05-26 02:37:35

标签: jquery checkbox checkboxlist

这是我的功能A 的脚本,用于检索复选框的属性,这就是它给我错误的状态:

var checkedCount = 0;

$('input.new-checkbox').each(function() {

     console.log('checked : ' + $(this).prop('checked')); // returned false
     console.log('disabled : ' + $(this).prop('disabled')); // returned false

     console.log('checked : ' + this.checked); // returned false
     console.log('disabled : ' + $(this).attr('disabled')); // returned undefined

     console.log('checked : ' + $(this).is(':checked')); // returned false

     console.log('disabled : ' + $(this).attr('disabled')); // returned undefined            
     console.log('checked : ' + $(this).attr('checked')); // returned undefined

     if($(this).prop('checked') == true)
     {
            checkedCount = checkedCount + 1;                    
     }

});

以下是功能B 的脚本我如何禁用并选中复选框,其中 arrayA 是一个数组:

$('input.new-checkbox').each(function() {

        for(var i=0; i<arrayA.length; i++)
        {                               
            if($(this).val() == arrayA[i])
            {           
                // set the checkbox checked
                $(this).prop('checked', true);

                // disable the checkbox
                $(this).prop('disabled', true);

            }                               

        }

    });

如果每个复选框的值等于数组中的值,则检查每个复选框,然后选中复选框并禁用它。

当我点击/选中复选框时,它会正确地返回道具('已检查')和道具('已禁用')的状态。但是当它已被禁用和检查时,它会为“已检查”和“已禁用”属性提供“假”。

有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

使用attr代替prop试试这个,

            // set the checkbox checked
            $(this).attr('checked', true);

            // disable the checkbox
            $(this).attr('disabled', true);