我如何确定在您所在的州选中了哪个复选框?
How can I determine which checkbox was selected in your state?
我正在使用 react-native-elements 复选框,我有 2 个复选框,我只想 select 其中一个,我设法做到了,但我正在尝试 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,我想在其中 post 数据,它有 maritalStatus
属性,我想根据复选框
看起来是异或运算。您需要通过查看单击按钮的过去状态来设置每个人的当前状态。
http://www.howtocreate.co.uk/xor.html
单身:
{single: !this.state.single, married: this.state.single}
已婚
{single:this.state.married, married: !this.state.married}
我认为您不需要为此管理两个状态。在这种情况下,一个人可以是已婚或单身。
所以你需要做这样的事情
如果您想在加载时取消选中这两个复选框,那么
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})}/>
希望这对你有用。 :)
确实存在三个条件。
- 用户没有选择框
- 用户选择单人
- 用户选择已婚。
如果您有验证,这意味着用户必须选中一个框,那么您可以不考虑第一个条件。由此,一旦用户选择了一个盒子,就可以通过仅了解其中一个盒子的状态来判断他们的选择是什么。因此,如果您有一个检查验证的按钮,您可以这样做。
<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
}
}}
/>
我正在使用 react-native-elements 复选框,我有 2 个复选框,我只想 select 其中一个,我设法做到了,但我正在尝试 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,我想在其中 post 数据,它有 maritalStatus
属性,我想根据复选框
看起来是异或运算。您需要通过查看单击按钮的过去状态来设置每个人的当前状态。
http://www.howtocreate.co.uk/xor.html
单身:
{single: !this.state.single, married: this.state.single}
已婚
{single:this.state.married, married: !this.state.married}
我认为您不需要为此管理两个状态。在这种情况下,一个人可以是已婚或单身。 所以你需要做这样的事情
如果您想在加载时取消选中这两个复选框,那么
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})}/>
希望这对你有用。 :)
确实存在三个条件。
- 用户没有选择框
- 用户选择单人
- 用户选择已婚。
如果您有验证,这意味着用户必须选中一个框,那么您可以不考虑第一个条件。由此,一旦用户选择了一个盒子,就可以通过仅了解其中一个盒子的状态来判断他们的选择是什么。因此,如果您有一个检查验证的按钮,您可以这样做。
<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
}
}}
/>