如何使用reactjs将一个表单值设置为另一个表单值?

时间:2018-07-29 00:28:21

标签: reactjs

我有一个接受用户输入的表格。表单具有一个切换为true和false的开关,如果为true,则为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, 
    })
}

1 个答案:

答案 0 :(得分:0)

您不能只分配它吗?

赞:

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, 
    })
}