动态生成的输入字段-如何访问值

时间:2018-07-06 03:02:28

标签: reactjs forms input onclick ref

我正在用reactjs开发一个spa。

我的一个组件映射到一个数组,并生成包含输入框的子组件,如下所示:

  <div>
       <textarea placeholder="Please add a response here." id={review._id} 
                        type="text" className={style.responseBox}/><br/>
       <button  onClick={this.submitResponse} className= 
                       {style.responseButton}>Submit your response</button>
 </div>

如何获取“ submitResponse”事件处理程序以访问字段id = {review._id}的值?

我想要这样的东西:

(其中“ id”是输入元素的ID)

submitResponse(event, id){
console.log('user submitted value at input field id", id);
console.log('the value entered by the user at that field is', this.event.id.target.value);
}

由于组件动态生成这些输入字段,因此我无法在状态中提前声明它们,否则我将使这些受控输入字段成为可能。

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以这样做。

        <button
            onClick={(event) => {
                this.submitResponse(event, review._id);
            }}
            className={style.responseButton}
        >
            Submit your response
        </button>
相关问题