gpt4 book ai didi

jquery - 为什么在禁用和检查时检查和禁用 Prop 返回 false?

转载 作者:行者123 更新时间:2023-12-01 05:41:33 24 4
gpt4 key购买 nike

这是我的函数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);

}

}

});

我检查每个复选框的值是否等于数组中的值,然后将复选框设置为选中并禁用它。

当我单击/选中复选框时,它会正确返回 prop('checked') 和 prop('disabled') 的状态。但是当它已经被禁用和检查时,它会给我“检查”和“禁用”属性“假”。

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

最佳答案

使用 attr 而不是 prop 试试这个,

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

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

关于jquery - 为什么在禁用和检查时检查和禁用 Prop 返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30448568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com