[英]Form validation with click event
我有一个带有验证的表格:
<form role="form" method="POST" action="" >
<input type="password" class="form-control top-buffer-sm" placeholder="Password" name="password" required autofocus></input>
<input type="email" class="form-control top-buffer-sm" placeholder="Email" name="email" required autofocus></input>
<div class="modal-footer">
<button id="createUser" type="submit" class="btn btn-primary">Create</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
我的提交按钮有一个ID,在提交表单之前,我需要执行其他一些功能:
$('#createUser').click(function(){
//check to see if validation passed
//do custom stuff
//now make ajax call
$.ajax();
});
但是如果表单无效,则仍会进行我的ajax调用,这当然会导致服务器错误。 单击提交按钮时,如何检查表单是否通过了验证? 谢谢
为什么不列出表单上的Submit事件?
<form role="form" method="POST" action="" id="myForm">
然后:
$('#myForm').on('submit', function(event) {
// Prevent form submission on click
event.preventDefault();
// Write the code the determins if the form is valid.
// `this` refers to the form.
if (formIsValidCondition) {
// do Ajax call here
} else {
// Do return a response to the user
return false;
}
});
这是我的答案:
$('#createUser').click(function(){
var valid = $("form").valid();
if (valid)///keep on trucking
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.