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

标签 javascript html checkbox

复选框类型的输入在触发其 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/

相关文章:

javascript - 语义 UI checkall 不起作用

Android:SimpleCursorAdapter 用法

javascript - jQuery.on ("click") 在 AngularJs 中不起作用

javascript - 带滚动的圆形 slider (jquery+css3)

javascript - 如何仅将对象的一个​​值设置为true,其余为false(可切换类别)

html - 超出范围时,带有数字的 AngularJS 输入未定义

javascript - 更改 RTE 内的类和样式属性(设计模式打开)

html - css 无法以正确的方式加载字体

html - 使用 Kendo UI HTML5 文本框搜索和过滤 GridView 中的值

android - 如何创建一个根据变化的变量显示其状态的复选框?