如何在其点击事件中使用一个复选框发送两个参数?

How to send two parameters using one checkbox on its click event?

我想要做的是,当用户点击复选框时,相应的 companyid 应该传递给我可以更新其激活状态的函数。但是我无法通过一种方法发送 2 个值。

我在各个网站上搜索过,仍然没有得到任何结果。

这就是我印刷公司的方式:

{this.state.allCompanies.map(com => (
                <tr>
                 <td>{com.cname} </td>
                  <td>
                    <a>
                        <input
                          type="checkbox"
                          name="active"
                          checked={com.is_active == 1 ? "true" : ""}
                          onClick={this.handleActivated(this, com.cid)}
                        />
                    </a>
                  </td>
                </tr>

这是我的 handleActivated 函数:

handleActivated(e, id) {
    const active = e.target.name;
    console.log(e.target.value);
    console.log(e.target.checked);

    this.setState({
      active: e.target.checked
    });

    // this.setState({  });
  }

这是我的状态。

this.state = {
      allCompanies: [],
      data: false,
      company: "",
      active: ""
    };
    this.handleActivated = this.handleActivated.bind(this);

我要的是handleActivated方法中的companyid

希望对你有所帮助

onClick={(e) => this.handleActivated(e, com.cid)}

将onclick改成这个

onClick={event=>this.handleActivated(event,com.cid)}

将函数更改为箭头函数

handleActivated =(e, id)=> {
    const active = e.target.name;
    console.log(e.target.value);
    console.log(e.target.checked);

    this.setState({
      active: e.target.checked
    });

    // this.setState({  });
  }

一旦你这样做了,你就可以从构造函数中删除它,因为它不再需要了。

this.handleActivated = this.handleActivated.bind(this);

这会给你更简洁的代码