admin管理员组文章数量:1425969
I want to submit a form through a button which is outside the form and do the validation to that form. I'm using the Form tag from react-bootstrap.
My code doesn't validate the form
<Form
noValidate
validated={validated}
onSubmit={e=> this.handleSubmit(e)}>
<Form.Control
required
placeholder="Product Name"
onChange={e => this.setState({name: e.target.value })}
pattern={"[A-Z a-z]{3,30}"}
/>
</Form>
<button type="button" value="send" onClick={(e) => this.handleSubmit(e)} className={"btn btn-primary"}>Save</button>
handleSubmit(event) {
const form = event.currentTarget;
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
else
this.AddProduct();
this.setState({ validated: true });
}
I want to submit a form through a button which is outside the form and do the validation to that form. I'm using the Form tag from react-bootstrap.
My code doesn't validate the form
<Form
noValidate
validated={validated}
onSubmit={e=> this.handleSubmit(e)}>
<Form.Control
required
placeholder="Product Name"
onChange={e => this.setState({name: e.target.value })}
pattern={"[A-Z a-z]{3,30}"}
/>
</Form>
<button type="button" value="send" onClick={(e) => this.handleSubmit(e)} className={"btn btn-primary"}>Save</button>
handleSubmit(event) {
const form = event.currentTarget;
if (form.checkValidity() === false) {
event.preventDefault();
event.stopPropagation();
}
else
this.AddProduct();
this.setState({ validated: true });
}
Share
Improve this question
asked May 7, 2019 at 14:51
isso kdisso kd
3995 silver badges19 bronze badges
3
- 1 The HTML5 placeholder attribute is not a substitute for the label element – Quentin Commented May 7, 2019 at 14:55
- 2 Consider refactoring. I can't think of a reason for your button to not be inside your form tag. Even if position of the elements are an issue...that's what css is for. – Elroy Jetson Commented May 7, 2019 at 14:55
- @ElroyJetson actually i want to make a form then a form then a button save for the whole page , this button must validate the first form only! – isso kd Commented May 7, 2019 at 15:12
1 Answer
Reset to default 4Ideally: Don't do that. Form elements are a useful structural element.
Failing that. Add a form
attribute to the button with the value equal to the id
attribute of the form.
form HTML5
The form element that the button is associated with (its form owner). The value of the attribute must be the id attribute of a
<form>
element in the same document. If this attribute is not specified, the<button>
element will be associated to an ancestor<form>
element, if one exists. This attribute enables you to associate<button>
elements to<form>
elements anywhere within a document, not just as descendants of<form>
elements.
— MDN
本文标签: javascriptHow to submit a form through a button outside the formStack Overflow
版权声明:本文标题:javascript - How to submit a form through a button outside the form? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745458438a2659213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论