在这种情况下如何处理复选框

How to handle checkbox in this case?

本文关键字:处理 复选框 这种情况下 何处理      更新时间:2023-09-26

我通过post请求发送一些数据到数据库,我在我的应用程序中有一个复选框,我需要处理一个state

我正在使用这个库,这让我有点困惑。

在渲染方法

中有我的复选框
<Checkbox value="Active" ref="Active" defaultChecked={true} onCheck={this._isChecked} />

和处理它的函数

  _isChecked = () => {
    console.log('Checked: ');
  }

我需要它默认为checked = true

现在,这里我有我正在使用的函数,以便将数据发送到post请求

  _addDealer = () => {
    CreateDealersActions.createDealer({
      DealerName : this.refs.DealerName.getValue(),
      CardId     : this.refs.CardId.getValue(),
      NickName   : this.refs.NickName.getValue(),
      Picture    : 'http://lorempixel.com/150/150/',
      Active     : // HERE I NEED TO SET THE VALUE OF THE CHECKBOX
      LegalId    : this.refs.LegalId.getValue(),
      TypeId     : this.refs.TypeId.getValue(),
    });
  }

查看该函数中的Active : // HERE I NEED TO SET THE VALUE OF THE CHECKBOX部分,我需要将复选框的值放在那里。

那么,为了在密钥Active中发送适当的数据,我应该知道什么,因为我使用这个库,我需要放state吗?或者还有别的办法吗?

我要回答这个问题,因为可能你们中的一些人不知道我正在使用的库,它是一个令人讨厌的东西,文档是可怕的。

你不需要设置初始状态,因为复选框组件已经附带了一个初始状态。因此,为了获得复选框的值,您不需要设置函数。有了这些,我所做的就足够了:

<Checkbox value="Active" ref="Active" defaultChecked={true} />
  _addDealer = () => {
    CreateDealersActions.createDealer({
      DealerName : this.refs.DealerName.getValue(),
      CardId     : this.refs.CardId.getValue(),
      NickName   : this.refs.NickName.getValue(),
      Picture    : 'http://lorempixel.com/150/150/',
      Active     : this.refs.Active.state.switched,
      LegalId    : this.refs.LegalId.getValue(),
      TypeId     : this.refs.TypeId.getValue(),
    });
  }

so: this.refs.Active.state.switched足以获得正确的复选框值