最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - onchange event on checkbox always shows this.value = on - Stack Overflow

programmeradmin1浏览0评论

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)">

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)">

Share Improve this question asked Apr 29, 2015 at 1:09 laggingreflexlaggingreflex 34.6k36 gold badges143 silver badges200 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

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

发布评论

评论列表(0)

  1. 暂无评论