对于复选框,我放置了一个函数,该函数将动态添加选中的属性,如下所示
if ($(_this).prop("checked") == true) {
$(_this).attr('checked', 'checked');
}
else if ($(_this).prop("checked") == false) {
$(_this).removeAttr('checked');
}
当它显示以下内容作为复选框的属性时
<center><input onclick=\"CheckboxIsChecked(this)\" type=\"checkbox\" checked=\"checked\"></center>
但是当函数捕获元素中是否存在已检查属性时,如下所示。它总是会返回 false。我的代码有什么问题吗?
$(List[i].Checkbox).prop("checked") ?真:假
最佳答案
您可以使用is(":checked")
来确定复选框是否被选中:
function CheckboxIsChecked(elem) {
console.log($(elem).is(":checked"));
return $(elem).is(":checked");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<center><input onclick="CheckboxIsChecked(this)" type="checkbox"></center>
关于javascript - JQuery - 复选框未返回 "checked"的正确值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46491422/