Inspect the checked
property of the element.
检查元素的checked
属性。
Say you have this checkbox:
说您有这个复选框:
<input type="checkbox" class="checkbox" />
You can see if it’s checked using
您可以查看是否已使用进行了检查
document.querySelector('.checkbox').checked
You can also check if looking for .checkbox:checked
does not return null
:
您还可以检查寻找.checkbox:checked
是否不返回null
:
document.querySelector('.checkbox:checked') !== null
but I think looking for .checked
is cleaner.
但我认为寻找.checked
更干净。
Do NOT use getAttribute()
looking for the checked
attribute value, because that’s always true if the checkbox is checked by default in this way:
不要使用getAttribute()
查找已checked
属性值,因为如果默认情况下以这种方式选中了此复选框,则始终为true:
<input type="checkbox" checked />
Also don’t check for the value
of a checkbox element. It’s always on
, regardless whether the checkbox is checked or not.
也不要检查复选框元素的value
。 它始终on
,无论是否选中该复选框。