简体   繁体   中英

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

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

 <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:

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

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.

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