Javascript 按钮-启用和禁用
我使用React Native创建了一个表单,但是我希望在TextInput为空时禁用record按钮,当填写完所有TextInput后,该按钮返回到enabledJavascript 按钮-启用和禁用,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 {
我该怎么做?你能给我举个例子吗?你可以这样做:
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}
/>
提交
);
}
}
基本上,您将文本输入的每个值存储在状态中,并在填充所有值时切换可触摸*
(也适用于按钮
)组件的禁用
属性。
在这里,你还可以做一些基本的验证,比如长度或匹配模式