<input type="checkbox" onChange={this._handleChartSelection.bind(this, el.graph_id)} /> {el.graph_name}
_handleChartSelection(chartId, checked){
let data = this.state.selectedChartId
if(checked)
data.push(chartId);
else{
let i = data.indexOf(chartId);
data.splice(i,1);
}
this.setState({
selectedChartId: data
});
console.log(this.state.selectedChartId);
}
我有多张带有复选框的卡,这些复选框都表明我正在将其推入阵列,如果我再次取消选中它,它也在将其推入阵列。
答案 0 :(得分:1)
获取checked
的方式需要更改
_handleChartSelection(chartId, e){
var checked = e.target.checked;
let data = this.state.selectedChartId
if(checked)
data.push(chartId);
else{
let i = data.indexOf(chartId);
data.splice(i,1);
}
this.setState({
selectedChartId: data
});
console.log(this.state.selectedChartId);
}
请尝试这个