<form onSubmit={this.handleSubmit}>
class YourComponent extends Component {
// stuff
handleSubmit(event) {
// do whatever you need here
// if you need to stop the submit event and
// perform/dispatch your own actions
event.preventDefault();
}
// more stuff
}
只需将一个处理程序传递给 onSubmit道具
其他框架/库
请参考框架的文档。
确认
您总是可以用 JavaScript 进行验证,但是对于 HTML5,我们也可以进行本地验证。
<!-- Must be a 5 digit number -->
<input type="number" required pattern="\d{5}">
window.onload = function () {
document.getElementById("frmSubmit").onsubmit = function onSubmit(form) {
var isValid = true;
//validate your elems here
isValid = false;
if (!isValid) {
alert("Please check your fields!");
return false;
}
else {
//you are good to go
return true;
}
}
}