我有一个表单,它接受用户的输入。该表单有一个切换为true和false的开关,如果为true,则设置相同的值,如果为false,则不会设置相同的值。然后我推入一个数组。它是一个常规的表单,表单的id与州的名称相对应,使用的都是输入类型。
constructor() {
this.state({
input1: '',
input2: '',
copyinput1: '',
copyinput2: '',
infoSame: false,
array: []
})
}
handleFormInput(event) {
const target = event.target;
const value = target.type === 'input' ? target.value : target.value
const id = target.id;
this.setState({
[id]:value
})
}
handleFormSubmit() {
if(infoSame === false) {
array.push({
input1: this.state.input1,
input2: this.state.input2,
copyinput1: this.state.copyinput1,
copyinput2: this.state.copyinput2
})
}
else {
//here is where I want input1 and input2 to be the values
//for copyinput1 and copyinput2
//How can this be achieved?
}
this.setState({
input1: '',
input2: '',
copyinput1: '',
copyinput2: '',
infoSame: false,
})
}
发布于 2018-07-29 08:40:13
你不能直接分配它吗?
像这样:
handleFormSubmit() {
if(infoSame === false) {
array.push({
input1: this.state.input1,
input2: this.state.input2,
copyinput1: this.state.copyinput1,
copyinput2: this.state.copyinput2
})
}
else {
//here is where I want input1 and input2 to be the values
//for copyinput1 and copyinput2
//How can this be achieved?
array.push({
input1: this.state.input1,
input2: this.state.input2,
copyinput1: this.state.input1,
copyinput2: this.state.input2
})
}
this.setState({
input1: '',
input2: '',
copyinput1: '',
copyinput2: '',
infoSame: false,
})
}
https://stackoverflow.com/questions/51576283
复制相似问题