简体   繁体   English

复选框上的onchange事件始终显示this.value = on

[英]onchange event on checkbox always shows this.value = on

An input of type checkbox when fires its onchange event the this.value is always set to on even when the checkbox is ticked off in which case I'd expect it to be off 当触发onchange事件时输入类型复选框this.value总是设置为on即使勾选了复选框,在这种情况下我希望它off

Is this the intended behavior? 这是预期的行为吗?

 <input type="checkbox" onchange="alert(this.value)"> 

short answer: do not use value to check checked status on checkboxes, use checked instead 简答:不要使用value来检查复选框上的选中状态,而是使用checked

 <input type="checkbox" onchange="alert(this.checked)"> 

and in case that you wonder why is this always-"on" value, there's a specification for that in HTML5 specification: 如果你想知道为什么这总是“开”值,那么在HTML5规范中有一个规范:

default/on 默认情况下/上

On getting, if the element has a value attribute, it must return that attribute's value; 获取时,如果元素具有value属性,则必须返回该属性的值; otherwise, it must return the string "on" . 否则, 它必须返回 字符串“on” On setting, it must set the element's value attribute to the new value. 在设置时,必须将元素的value属性设置为新值。

http://www.w3.org/TR/html5/forms.html#dom-input-value-default-on http://www.w3.org/TR/html5/forms.html#dom-input-value-default-on

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM