Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按钮-启用和禁用_Javascript_React Native - Fatal编程技术网

Javascript 按钮-启用和禁用

Javascript 按钮-启用和禁用,javascript,react-native,Javascript,React Native,我使用React Native创建了一个表单,但是我希望在TextInput为空时禁用record按钮,当填写完所有TextInput后,该按钮返回到enabled 我该怎么做?你能给我举个例子吗?你可以这样做: class Form extends Component { constructor(props) { super(props); this.state = { name: '', email: '' }; } render() { const {

我使用React Native创建了一个表单,但是我希望在TextInput为空时禁用record按钮,当填写完所有TextInput后,该按钮返回到enabled


我该怎么做?你能给我举个例子吗?

你可以这样做:

class Form extends Component {
  constructor(props) {
    super(props);
    this.state = { name: '', email: '' };
  }

  render() {
    const { name, email } = this.state;

    return (
      <View>
        <TextInput
          onChangeText={name => this.setState({ name })}
          value={name}
        />
        <TextInput
          onChangeText={email => this.setState({ email })}
          value={email}
        />
        <TouchableHighlight disabled={!name || !email}>
          Submit
        </TouchableHighlight>
      </View>
    );
  }
}
类表单扩展组件{
建造师(道具){
超级(道具);
this.state={name:'',电子邮件:'};
}
render(){
const{name,email}=this.state;
返回(
this.setState({name})}
值={name}
/>
this.setState({email})}
值={email}
/>
提交
);
}
}
基本上,您将
文本输入的每个值存储在状态中,并在填充所有值时切换
可触摸*
(也适用于
按钮
)组件的
禁用
属性。
在这里,您还可以执行一些基本验证,如长度或匹配模式。

您可以执行以下操作:

class Form extends Component {
  constructor(props) {
    super(props);
    this.state = { name: '', email: '' };
  }

  render() {
    const { name, email } = this.state;

    return (
      <View>
        <TextInput
          onChangeText={name => this.setState({ name })}
          value={name}
        />
        <TextInput
          onChangeText={email => this.setState({ email })}
          value={email}
        />
        <TouchableHighlight disabled={!name || !email}>
          Submit
        </TouchableHighlight>
      </View>
    );
  }
}
类表单扩展组件{
建造师(道具){
超级(道具);
this.state={name:'',电子邮件:'};
}
render(){
const{name,email}=this.state;
返回(
this.setState({name})}
值={name}
/>
this.setState({email})}
值={email}
/>
提交
);
}
}
基本上,您将
文本输入的每个值存储在状态中,并在填充所有值时切换
可触摸*
(也适用于
按钮
)组件的
禁用
属性。 在这里,你还可以做一些基本的验证,比如长度或匹配模式