9

这是我的 HTML:

<input id="test" type="checkbox" checked="">

这是 Firebug 的摘录:

>>> test
<input id="test" type="checkbox" checked="">

>>> test.checked = false
false

>>> test
<input id="test" type="checkbox" checked="">

嗯......我错过了什么,或者最后一行不应该阅读以下内容吗?

<input id="test" type="checkbox">

UI 方面,当我执行该行时,该复选框确实取消选中checked = false

无论如何,如果对此有一些合理的解释,那么从 JavaScript 中取消选中复选框的正确方法是什么checked = false

4

3 回答 3

23

value属性input type="text"和 的checked属性selected,input type="checkbox"radio对应option于表单域的初始值,而不是用户或脚本设置的当前值。因此,更改checked属性不会更改属性值,设置checked属性不会更改将与表单一起提交的真实可见值。

checked="checked"属性对应于defaultChecked DOM 属性,而不是checked属性。同样,该value="..."属性对应于defaultValue

(注意这里有 IE 陷阱,因为 IE 不知道属性和属性之间的区别。)

于 2009-12-04T22:11:36.013 回答
-2

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.

于 2009-12-05T00:15:54.343 回答
-3

checked = '',我相信是正确的。checked = false我怀疑浏览器在您这样做并执行等效操作时试图变得友好, checked = ''.

于 2009-12-04T21:53:49.600 回答