2 回答

TA貢獻1827條經驗 獲得超4個贊
最好按如下方式執行(它允許您動態創建輸入,如果您不需要動態輸入,也可以使用相同的技術)
constructor(props) {
super(props);
this.state = {
education: ["", ""] // I've added 2 items to create 2 inputs
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
const education = [...this.state.education];
education[e.target.id] = e.target.value;
this.setState({
education: education
});
}
render() {
return (
<div>
{
this.state.education.map((item, index) => (
<input
id={index}
type="text"
name="education"
value={this.state.education[index]}
onChange={this.handleChange}
class="form-control"
/>
))
}
</div>
);
}

TA貢獻1852條經驗 獲得超7個贊
在你的第二個輸入中,你(我認為是錯誤的)()
在你的this.handleChange
函數之后添加了。這意味著該函數將立即被調用,而不會被調用 onChange。
- 2 回答
- 0 關注
- 146 瀏覽
添加回答
舉報