如何在react js中获取具有相同属性名称的数组中输入标签的值

我想获取 education[0] 中第一个输入标签的值和 education[1] education 中第二个输入标签的值是数组。


<input  type="text" name="education" value={this.state.education[0]} onChange={this.handleChange}  

class="form-control"  />

<input type="text"  name="education" value={this.state.education[1]} onChange={ 

this.handleChange()} class="form-control"/>


叮当猫咪
浏览 96回答 2
2回答

GCT1015

最好按如下方式执行(它允许您动态创建输入,如果您不需要动态输入,也可以使用相同的技术)constructor(props) {&nbsp; &nbsp; super(props);&nbsp; &nbsp; this.state = {&nbsp; &nbsp; &nbsp; education: ["", ""] // I've added 2 items to create 2 inputs&nbsp; &nbsp; };&nbsp; &nbsp; this.handleChange = this.handleChange.bind(this);&nbsp; }handleChange(e) {&nbsp; &nbsp; const education = [...this.state.education];&nbsp; &nbsp; education[e.target.id] = e.target.value;&nbsp; &nbsp; this.setState({&nbsp; &nbsp; &nbsp; education: education&nbsp; &nbsp; });&nbsp; }render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.state.education.map((item, index) => (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id={index}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="text"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name="education"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={this.state.education[index]}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={this.handleChange}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="form-control"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; ))&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );}

慕姐4208626

在你的第二个输入中,你(我认为是错误的)()在你的this.handleChange函数之后添加了。这意味着该函数将立即被调用,而不会被调用 onChange。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5