【发布时间】:2020-01-11 04:21:43
【问题描述】:
此复选框 [ 看起来像切换按钮] 基于数据生成。 我想在数组中获取检查值。
代码
{this.state.customersmsselect.map((e, key) => {
if(e.isactive == "ON"){
return (
<div key={key}>
<div className="form-group col-md-8">
<label className="col-md-4 control-label">{e.notificationtype}</label>
<div className="col-md-4 smart-form">
<label className="toggle">
<input type="checkbox" id={"smscheckbox" + key} value={e.isactive + "," + e.smstemplateid} name={"checkbox-toggle"+e.smstemplateid} onChange={this.onChangeFavorite} defaultChecked />
<i data-swchoff-text='OFF' data-swchon-text='ON' ></i>
</label>
</div>
</div>
<div style={{ margin: 10 }}> </div>
</div>
);
我的 onChange 方法
如果点击那个按钮,我会得到那个按钮的名字。
onChangeFavorite(event) {
if(!event.target.checked){
console.log(event.target.name);
}
};
我的 customersmsselect 数组看起来像
0: {smstemplateid: "1", notificationtype: "Invoice Details", isactive: "ON"}
1: {smstemplateid: "2", notificationtype: "Payment Thank-You", isactive: "ON"}
2: {smstemplateid: "3", notificationtype: "Daily Closing Balance", isactive: "OFF"}
3: {smstemplateid: "4", notificationtype: "Monthly Closing Balance", isactive: "ON"}
问题
当我点击按钮时,我想在一个数组中获取所有选中的值。
【问题讨论】:
-
我想要一个反应的解决方案,我不想用Jquery来操作DOM
-
您必须在 OnChange 事件中捕获 CheckBox 状态并将其存储在状态对象中。然后在需要时从 State 获取值
标签: javascript reactjs checkbox