【发布时间】:2021-11-15 09:48:43
【问题描述】:
好吧,我有一个复选框,当被选中时,它应该保存一些值,当被取消选中时,它应该删除我刚刚取消选中的那个值。从我在Material UI: Checkbox 中看到的内容来看,“受控”部分中有一个“简单的方法”,但它没有用,所以我尝试了其他方法,但也没有用,现在我被卡住了。我看到了类似的问题并尝试了Stackoverflow: How to call 2 functions on onclick of a checkbox?。但显然这也不起作用,因为它来自常规复选框,但我想使用 Material UI 中的复选框。这就是我所拥有的:
Relevant code
const [studentID, setStudentID] = useState([])
const setStudent = (estudiante) => {
var checkBox = document.getElementById('checkBox');
if(checkBox.ariaChecked == true){
console.log("Save data")
let data = studentID;
data.push(estudiante);
setStudentID(data);
console.log(studentID)
}
else{
console.log("Delete data")
}
}
<Checkbox
color = "primary"
id = "checkBox"
onChange = {() => setStudent(estudiante.uid)}
inputProps={{ 'aria-label': 'controlled' }}
/>
我尝试根据之前从其他帖子中看到的建议对其进行调整,但它不起作用,它只通过一个选项(我没有创建代码来删除刚刚添加的 console.log 的值)
如果我反转它们,这就是我尝试保存数据时的样子(这确实有效,但因为它只做一个或另一个重复)
非常感谢任何提示、建议、文档或帮助。如果需要其他内容,请告诉我。
【问题讨论】:
标签: reactjs checkbox material-ui