如何在React中保存多个复选框值以及选择值

问题描述

我有一个带有“反应”复选框的表格,然后从“反应选择”中进行选择。我想将复选框中的值和选择选项中的值一起保存为对象中的数组。现在,复选框值分别保存在currency中,而选择值分别保存在currencies中,但是我希望所有值都保存在currencies中。这可能吗? 希望它不要太混乱。 到目前为止,我的代码看起来像这样。

对象状态:

constructor(props) {
        super(props)
        this.state = {
            currencies: [],currency: '',}

复选框:

  handleChange = input => event => {
        console.log(input);
        console.log(event.target.value);
        this.setState({[input] : event.target.value})
    }

\\\\\\\\\\\\\\\\

     <label style={label}>{strings.currency}</label>
       <div style={checkBoxDiv}>
         {commonCurrencies.map((currency,index) => (
       <label style={checkBoxLabel} key={index}>
       <input style={checkBox} type="checkBox" onChange={value => this.handleChange('currency')} value={currency} required> 
       </input>
         {currency}
     </label>
       ))}
       </div>

反应选择:

 handleSelectChange (field,options) {
        
        if(options.length === undefined) {
            this.setState({[field]: options.value});
        } else {
                for (var item = 0; item < options.length; item++) {
                    values.push(options[item].value);
                }
            }

            this.setState({[field]: values});
        }

\\\\\\\\\\\\\\\\\\\\\\\\\

  handleSelectChange(field,option) {
        this.props.handleSelectChange(field,option);
        return option
    }
\\\\\\\\\\\\\\\\\\

<Select styles={customStyles} menuColor='#502272' isMulti options={currencyData} placeholder={strings.searchCurrency} onChange={value => this.handleSelectChange("currenciesSendRetrieve",value)} value={values.currencyData}></Select>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)