微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何在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 举报,一经查实,本站将立刻删除。