复选框类型的输入在触发其 onchange
事件时 this.value
始终设置为 on
即使复选框被勾选在这种情况下,我希望它是 off
这是预期的行为吗?
<input type="checkbox" onchange="alert(this.value)">
最佳答案
简短回答:不要使用 value
来检查复选框的选中状态,而是使用 checked
<input type="checkbox" onchange="alert(this.checked)">
如果您想知道为什么这个值总是“开启”,HTML5 规范中对此有一个规范:
default/on
On getting, if the element has a value attribute, it must return that attribute's value; otherwise, it must return the string "on". On setting, it must set the element's value attribute to the new value.
http://www.w3.org/TR/html5/forms.html#dom-input-value-default-on
关于javascript - 复选框上的 onchange 事件始终显示 this.value = on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29932668/