【问题标题】:Toggle button based on input (Native Base)根据输入切换按钮(Native Base)
【发布时间】:2020-11-19 14:40:17
【问题描述】:

当两个或一个输入为空时,我想禁用按钮。顺便说一句,它是 Native Base 框架。

    <View style={styles.personal} key={data}>
        <Item floatingLabel>
          <Label>Your name:</Label>
          <Input />
        </Item>
        <Item floatingLabel style={{marginVertical: 20}}>
          <Label>Your age:</Label>
          <Input keyboardType="numeric"/>
        </Item>
        <Button style={{alignSelf: 'center'}} rounded disabled={false}
          onPress={() => this.refs.swiper.scrollBy(1)}>
            <Text style={{width: '70%', textAlign: 'center'}}>Next</Text>
          </Button>
      </View>

我尝试过的:

state = {
  inputName: "",
  inputAge: "",
}
<Input value={this.setState({inputName: value})}/> 
<Input value={this.setState({inputAge: value})} keyboardType="numeric"/>
<Button style={{alignSelf: 'center'}} rounded 
          disabled={this.state.inputName==="" & this.state.inputAge==="" ? true:false }
          onPress={() => this.refs.swiper.scrollBy(1)}>
            <Text style={{width: '70%', textAlign: 'center'}}>Next</Text>
        </Button>

变量值错误

【问题讨论】:

    标签: javascript reactjs react-native native-base


    【解决方案1】:

    您在这种情况下错过了额外的&amp;

    你的情况应该是这样的,

    disabled={this.state.inputName==="" || this.state.inputAge==="" ? true:false }
    

    因为对于逻辑与运算,只有当两个输入都为空时,结果才会为真。如果任何输入不为空,则结果将为 false,它将启用按钮。所以你需要使用逻辑或运算,这样当任何输入为空时,条件都会返回true。

    && 是逻辑与运算

    & 是按位与

    在这里找到& and && 之间的区别。

    您可以执行以下操作以将输入值保存在状态中。

    <Input onChangeText={val => this.setState({ inputName: val })} value={this.state.inputName}/> 
    <Input onChangeText={val => this.setState({ inputAge: val })} value={this.state.inputAge} keyboardType="numeric"/>
    

    【讨论】:

    • 哦,我明白了,但错误仍然存​​在“找不到变量值”
    • 如何将 Input(它是 TextInput)的值分配给 state?
    • 您应该使用 onChange 事件处理程序来更新状态中的输入值。我正在用那部分更新我的代码。
    • 我对两个输入都做了 this.setState({inputName: value})} />,但是如果其中一个输入具有值,则启用按钮,但是我需要同时检查
    • 是的,我已经更换了
    猜你喜欢
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    相关资源
    最近更新 更多