此复选框[看起来像切换按钮]是根据数据生成的。 我想在数组中获取检查值。
代码
{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);
}
};
我的客户msselect阵列看起来像
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"}
问题
我想在单击按钮时获取数组中所有检查的值。
答案 0 :(得分:1)
您可以尝试像这样使用ref:
{this.state.customersmsselect.map((e, key) => {
$('#s').val(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 ref={node => { this.checkBoxes[key] = node; }} />
<i data-swchoff-text='OFF' data-swchon-text='ON' ></i>
</label>
</div>
</div>
<div style={{ margin: 10 }}> </div>
</div>
);
然后您可以将this.checkBoxes称为常规数组
答案 1 :(得分:1)
您的输入字段:
<input type="checkbox" id={"smscheckbox" + key} value={e.isactive} name={"checkbox-toggle"+e.smstemplateid} onChange={(event) => this.onChangeFavorite(event, key)} checked={e.isactive === "ON"} />
您的onChange事件:
onChangeFavorite(event, index) {
const shouldBeOnOrOff =
this.state.customersmsselect[index].isactive === "ON" ? "OFF" : "ON";
this.setState({
customersmsselect: this.state.customersmsselect.map((item, i) =>
index === i ? { ...item, isactive: shouldBeOnOrOff } : item
)
});
}
这将为您提供一系列当前处于打开状态的对象:
this.state.customersmsselect.filter((item) => item.isactive === "ON");
这将为您提供一个当前为ON的ID数组,我认为这是您想要的值?
this.state.customersmsselect.filter((item) => item.isactive === "ON").map((item) => item.smstemplateid);
答案 2 :(得分:1)
在这里,我们可以改用受控输入。然后,我们需要绑定选中的属性,并在onChange处理程序中设置状态。 checked={e.isactive === "ON"}
根据值检查复选框。 onChange={()=>this.onChangeFavorite(e)}
将附加事件处理程序以及正在映射的customermsselect项。
x.isactive = x.isactive === "OFF" ? "ON" : "OFF";
this.setState({
customersmsselect: this.state.customersmsselect
});
对输入进行必要的检查。
{this.state.customersmsselect.map((e, key) => {
$('#s').val(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(e)} checked={e.isactive === "ON"} />
<i data-swchoff-text='OFF' data-swchon-text='ON' ></i>
</label>
</div>
</div>
<div style={{ margin: 10 }}> </div>
</div>
);
然后
onChangeFavorite(x) {
//Here now we are directly changing the array element value.
//Since its in the state when we change and assign it will automatically
//update the controlled components.
x.isactive = x.isactive === "OFF" ? "ON" : "OFF";
this.setState({
customersmsselect: this.state.customersmsselect
});
};