【问题标题】:How can I determine which checkbox was selected in your state?如何确定在您所在的州选中了哪个复选框?
【发布时间】:2019-03-03 18:51:15
【问题描述】:

我正在使用 react-native-elements 复选框,我有 2 个复选框,我想只选择其中一个,我设法做到了,但我试图 console.log 仅选中复选框,但它不起作用,因为他们有两种不同的状态,那么我如何确定使用该状态在我的应用程序中选中了哪个框?这是代码:

初始状态:

state: {
  single: false,
  married: false
}

复选框:

 <CheckBox title="Single"
              checked={this.state.single}
              onPress={() => this.setState({ single: !this.state.single, 
              married: false})}/>

 <CheckBox title="Married"
              checked={this.state.married}
              onPress={() => this.setState({ married: !this.state.married, 
              single: false})}/>

我有一个 api,我想在其中发布数据,它有 maritalStatus 属性,我想根据复选框发送已婚或单身作为字符串值

【问题讨论】:

  • 你有单身和已婚的状态。被选中的将是真实的。所以你已经知道哪个复选框被选中了。
  • @HemantParasha 好的,但是如果我想在 post 调用中发送 true,我该怎么做?
  • 我认为您需要重新表述您的问题,并提供更多关于您真正想要实现的目标的信息,并提供与此相关的代码。现在一切都很模糊。
  • @Andrew console.log(this.state.married) 或 console.log(this.state.single),它们根据选中的返回 true 或 false,但事情是我只想要仅不使用 2 个状态的真实值
  • @Andrew 这是我的意思,我不知道如何检查

标签: javascript react-native checkbox state react-native-elements


【解决方案1】:

确实存在三个条件。

  1. 用户尚未选择框
  2. 用户选择single
  3. 用户选择已婚。

如果您有验证,这意味着用户必须选中一个框,那么您可以忽略第一个条件。由此,一旦用户选择了一个框,就可以通过仅知道其中一个框的状态来判断他们的选择是什么。所以如果你有一个检查验证的按钮,你可以做这样的事情。

<Button 
  title={'check married status'}
  onPress={() => {
   if (!this.state.single && !this.state.married) {
     alert('Please check a box)
   } else {
     // we only need to check one of them
     let marriedStatus = this.state.married ? 'married' : 'single';
     alert(`You are ${marriedStatus}`)
     // then you can do what you want with the marriedStatus here
   }
  }}
/>

【讨论】:

    【解决方案2】:

    看起来它们是异或运算。您需要通过查看单击按钮的过去状态来设置每个人的当前状态。

    http://www.howtocreate.co.uk/xor.html

    单身:

    {single: !this.state.single, married: this.state.single}
    

    结婚

    {single:this.state.married, married: !this.state.married}
    

    【讨论】:

      【解决方案3】:

      我认为您不需要为此管理两个状态。在这种情况下,一个人可以结婚也可以单身。 所以你需要做这样的事情

      如果你想让这两个复选框在加载时都处于未选中状态

      state: {
        single: void 0,
      }
      
      
      <CheckBox title="Single"
                    checked={this.state.single}
                    onPress={() => this.setState({ single: !this.state.single})}/>
      
       <CheckBox title="Married"
                    checked={this.state.single !== undefined && !this.state.single}
                    onPress={() => this.setState({ married: !this.state.single})}/>
      

      或者如果有一个检查然后

      state: {
        single: true,
      }
      
      
      <CheckBox title="Single"
                    checked={this.state.single}
                    onPress={() => this.setState({ single: !this.state.single})}/>
      
       <CheckBox title="Married"
                    checked={!this.state.single}
                    onPress={() => this.setState({ married: !this.state.single})}/>
      

      希望这对你有用。 :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 2011-06-12
        • 1970-01-01
        • 1970-01-01
        • 2010-12-26
        • 2011-10-27
        • 1970-01-01
        相关资源
        最近更新 更多