提交表单后反应显示消息
React displaying message after submitting the form
尝试创建一个简单的表单,我正在使用语义 ui 反应。问题是提交表单后我无法在屏幕上显示任何消息。
<Form.TextArea
required
label="Description"
name="description"
placeholder="Your description..."
value={this.state.description}
error={this.state.descriptionError}
onChange={this.inputChange}
/>
<Form.Button
fluid
color="blue"
disabled={
!this.state.term
|| !this.state.description
}
>Submit</Form.Button>
{!this.state.formError
?
<Message
success
header="Form completed"
content="Thank you for your contribution."
/>
:
<Message
error
header="Missing fields!"
list={['All fields must be filled.']}
/>
}
</Form>
编辑:FormError 部分在这里。感谢您的答复。我会尝试所有的
inputChange = (e, {name, value}) => this.setState({[name]: value})
formSubmit = (e) => {
e.preventDefault();
let error = false;
if(this.state.description.length < 10){
this.setState({descriptionError: true})
error=true
}else{
this.setState({descriptionError: false})
}
if(error){
this.setState({formError: true})
return
}
this.setState({formError: false})
}
像这样修改您的代码:
<Form success={this.state.formSuccess} error={this.state.formError}>
<Message
success
header="Form completed"
content="Thank you for your contribution."
/>
<Message
error
header="Missing fields!"
list={['All fields must be filled.']}
/>
<Form.TextArea
required
label="Description"
name="description"
placeholder="Your description..."
value={this.state.description}
error={this.state.descriptionError}
onChange={this.inputChange}
/>
<Form.Button
fluid
color="blue"
disabled={
!this.state.term
|| !this.state.description
}
onClick={this.handleSubmit}
>Submit</Form.Button>
</Form>
然后像这样创建一个submitHandler
:
submitHandler = () => {
// Here do your stuff on submit
if(error){
this.setState({formError: true, formSuccess: false});
}
else{
this.setState({formError: false, formSuccess: true});
}
}
希望对您有所帮助!
这是工作代码 link - https://codesandbox.io/s/semantic-ui-react-responsive-navbar-drvf7?fontsize=14
您只需将状态值更新为 true 或 false,然后查看它工作正常的消息。
此外,在 <Message/>
组件中使用 positive
表示成功,使用 negative
表示错误
尝试创建一个简单的表单,我正在使用语义 ui 反应。问题是提交表单后我无法在屏幕上显示任何消息。
<Form.TextArea
required
label="Description"
name="description"
placeholder="Your description..."
value={this.state.description}
error={this.state.descriptionError}
onChange={this.inputChange}
/>
<Form.Button
fluid
color="blue"
disabled={
!this.state.term
|| !this.state.description
}
>Submit</Form.Button>
{!this.state.formError
?
<Message
success
header="Form completed"
content="Thank you for your contribution."
/>
:
<Message
error
header="Missing fields!"
list={['All fields must be filled.']}
/>
}
</Form>
编辑:FormError 部分在这里。感谢您的答复。我会尝试所有的
inputChange = (e, {name, value}) => this.setState({[name]: value})
formSubmit = (e) => {
e.preventDefault();
let error = false;
if(this.state.description.length < 10){
this.setState({descriptionError: true})
error=true
}else{
this.setState({descriptionError: false})
}
if(error){
this.setState({formError: true})
return
}
this.setState({formError: false})
}
像这样修改您的代码:
<Form success={this.state.formSuccess} error={this.state.formError}>
<Message
success
header="Form completed"
content="Thank you for your contribution."
/>
<Message
error
header="Missing fields!"
list={['All fields must be filled.']}
/>
<Form.TextArea
required
label="Description"
name="description"
placeholder="Your description..."
value={this.state.description}
error={this.state.descriptionError}
onChange={this.inputChange}
/>
<Form.Button
fluid
color="blue"
disabled={
!this.state.term
|| !this.state.description
}
onClick={this.handleSubmit}
>Submit</Form.Button>
</Form>
然后像这样创建一个submitHandler
:
submitHandler = () => {
// Here do your stuff on submit
if(error){
this.setState({formError: true, formSuccess: false});
}
else{
this.setState({formError: false, formSuccess: true});
}
}
希望对您有所帮助!
这是工作代码 link - https://codesandbox.io/s/semantic-ui-react-responsive-navbar-drvf7?fontsize=14
您只需将状态值更新为 true 或 false,然后查看它工作正常的消息。
此外,在 <Message/>
组件中使用 positive
表示成功,使用 negative
表示错误