【问题标题】:How to pass two arguments in OnChange? [duplicate]如何在 OnChange 中传递两个参数? [复制]
【发布时间】:2021-06-21 11:22:29
【问题描述】:

我用我的数据自动创建一个表,我需要将两个值传递给函数onPred1Change(),我需要输入值和循环计数器k

我该怎么做?

<Table striped bordered hover>
                      <thead>
                        <tr>
                          <th>#</th>
                          <th>Prediction</th>                         
                        </tr>
                      </thead>
                      <tbody>
                        {Object.keys(this.state.jsondata.label).map(k =>
                            <tr>
                                <td>{k}</td>
                                <td>                
                                    <input
                                        id={'pred' + k}
                                        className="form-control"
                                        type="text"
                                        value={this.state.jsondata.prediction[k]}
                                        onChange={e => this.onPred1Change(e.target.value)}
                                    />          
                                </td>
                            </tr>
                        )}                  
                      </tbody>
                    </Table>

因为我需要k 来查找数据的索引,而value 来修改这些数据。

这是我使用k 的快速示例:

<Button variant="success" onClick={() => { this.validatePred2(k) }}>Validate <Check/></Button>

【问题讨论】:

    标签: reactjs input


    【解决方案1】:

    用函数包裹它,但实际上你已经做得很好了。 只需要在onPred1Change 上传递另一个参数

    <input
          id={'pred' + k}
          className="form-control"
          type="text"
          value={this.state.jsondata.prediction[k]}
          onChange={(e) => this.onPred1Change(e.target.value, k)}
    />          
    

    之后,您需要更改 onPred1Change 以同时接受两个参数。

    所以它可以是这样的:

    const onPred1Change = (val, k) => {
      console.log(`get the ${val} and ${k}`)
    }
    

    【讨论】:

    • 是的,非常感谢:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2014-10-09
    • 2011-06-28
    • 2012-01-06
    相关资源
    最近更新 更多