Here's my HTML:
<input id="test" type="checkbox" checked="">
Here's a Firebug excerpt:
>>> test
<input id="test" type="checkbox" checked="">
>>> test.checked = false
false
>>> test
<input id="test" type="checkbox" checked="">
Um...am I missing something, or should that last line not read the following?
<input id="test" type="checkbox">
UI-wise, the checkbox does indeed uncheck when I execute the checked = false
line.
Anyway, if there's some legitimate explanation for this, then what's the proper way to uncheck a checkbox from JavaScript, if not checked = false
?
The value
attribute of input type="text"
and the checked
or selected
attributes of input type="checkbox"
, radio
and option
correspond to the initial value of the form field, not the current value the user or script has set. Consequently changing the checked
property does not alter the attribute value, and setting the checked
attribute does not alter the real visible value that's going to be submitted with the form.
The checked="checked"
attribute corresponds to the defaultChecked
DOM property , not the checked
property. Similarly, the value="..."
attribute corresponds to defaultValue
.
(Note there are IE gotchas here due to IE not knowing the difference between a property and an attribute.)
You may be expecting Firebug to display value information similarly to how style
is updated in the HTML inspection pane. However, input
, select
, option
, and textarea
do not behave the same way and values will not be updated in this pane and will always display the original values (those at page render time). If the UI is updating, then you know you're doing it right.
checked = ''
, is correct I believe. I suspect the browser is trying to be friendly when you do checked = false
and doing the equivalent action, checked = ''
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.