【发布时间】:2021-10-10 12:45:43
【问题描述】:
基本上,现在每次使用新值检查复选框时,我都会在控制台中打印一个新数组,例如: 结果[真、假、假]; = 如果第一个是唯一选中的复选框。
现在我想在 SimpleCard 反应组件中显示相应的文本,但每次它必须相应地更改为每次我选择或取消选择其中一个复选框时创建的新数组。 比如:第一个检查,第二个没有检查,第三个没有检查等等......
希望我能够向您解释。
constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);
//this.showlive = this.showlive.bind(this)
const checkboxList = [
{
id: "blu",
name: "blu",
value: "blu",
dimension: "sm",
label: "blu",
inputClass: [],
labelClass: [],
event: { onChange: this.onChange },
extraProps: { visible: true, checked: false }
},
{
id: "red",
name: "red",
value: "red",
dimension: "sm",
label: "red",
inputClass: [],
labelClass: [],
event: { onChange: this.onChange },
extraProps: { visible: true, checked: true }
},
{
id: "yellow",
name: "yellow",
value: "yellow",
dimension: "sm",
label: "yellow",
inputClass: [],
labelClass: [],
event: { onChange: this.onChange },
extraProps: { visible: true, checked: false }
}
]
this.state = {
error: null,
checkboxList: checkboxList
}
};
componentDidMount() {
}
onChange(event) {
const checkboxList = this.state.checkboxList.map((elem) => {
if (elem.name === event.target.name)
elem.extraProps.checked = event.target.checked
return elem
})
this.setState({
checkboxList: checkboxList,
SimpleCard: Body })
}
render(){
let i;
let Risultati = [];
for(i=0; i<3; i++){
let v = this.state.checkboxList[i].extraProps.checked;
Risultati.push(v)
return Risultati;
}
console.log(Risultati);
const legend = "Checkbox"
return (
<div>
<Checkbox
checkboxList={this.state.checkboxList}
legend={legend}
></Checkbox>
<SimpleCard
title="Colors selected"
titleHeading="h2"
data={<p> Data</p>}
body={ <p> Here I want the text to be displayed </p>}
>
{" "}
</SimpleCard>
</div>
);
};
};
【问题讨论】:
标签: javascript reactjs checkbox state onchange