首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用reactjs将一个表单值设置为另一个表单值?

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

Stack Overflow用户
提问于 2018-07-29 08:28:21
回答 1查看 19关注 0票数 -1

我有一个表单,它接受用户的输入。该表单有一个切换为true和false的开关,如果为true,则设置相同的值,如果为false,则不会设置相同的值。然后我推入一个数组。它是一个常规的表单,表单的id与州的名称相对应,使用的都是输入类型。

代码语言:javascript
复制
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, 
    })
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-29 08:40:13

你不能直接分配它吗?

像这样:

代码语言:javascript
复制
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, 
    })
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51576283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档