React Materialise 开关始终具有值 "on"
React Materialize Switch Always Has the Value "on"
我正在使用直接来自该网站的 React Switch 组件:http://react-materialize.github.io/react-materialize/?path=/story/components-switch--switch。我像这样把它放到我的 SideNav 中:
<Switch
id="Switch-11"
offLabel="Off"
onChange={(e) => exampleFunction(e)}
onLabel="On"
/>
我这样做是为了在单击开关时调用“exampleFunction()”,其中包含以下内容:
const exampleFunction = (e) => {
console.log(e.target.value)
}
问题是,当我查看控制台的开关值时,它总是“打开”,即使我多次按下它也是如此。我有点迷失为什么会这样。任何意见,将不胜感激。谢谢
如果想知道checkbox是否被选中,需要使用e.target.checked
,查看source code from materialize, we can see the onChange is passed directly to the input element, so you need to use the checked
attribute of the input element.
value
属性具有以下定义:
The value property sets or returns the value of the value attribute of a checkbox.
For checkboxes, the contents of the value property do not appear in the user interface. The value property only has meaning when submitting a form. If a checkbox is in checked state when the form is submitted, the name of the checkbox is sent along with the value of the value property (if the checkbox is not checked, no information is sent).
我正在使用直接来自该网站的 React Switch 组件:http://react-materialize.github.io/react-materialize/?path=/story/components-switch--switch。我像这样把它放到我的 SideNav 中:
<Switch
id="Switch-11"
offLabel="Off"
onChange={(e) => exampleFunction(e)}
onLabel="On"
/>
我这样做是为了在单击开关时调用“exampleFunction()”,其中包含以下内容:
const exampleFunction = (e) => {
console.log(e.target.value)
}
问题是,当我查看控制台的开关值时,它总是“打开”,即使我多次按下它也是如此。我有点迷失为什么会这样。任何意见,将不胜感激。谢谢
如果想知道checkbox是否被选中,需要使用e.target.checked
,查看source code from materialize, we can see the onChange is passed directly to the input element, so you need to use the checked
attribute of the input element.
value
属性具有以下定义:
The value property sets or returns the value of the value attribute of a checkbox.
For checkboxes, the contents of the value property do not appear in the user interface. The value property only has meaning when submitting a form. If a checkbox is in checked state when the form is submitted, the name of the checkbox is sent along with the value of the value property (if the checkbox is not checked, no information is sent).