我创建了一个注册表单并将其input
字段设置为require
属性,但它不能与ajax一起使用
<div class="form-group">
<input required type="text" id="First_name" placeholder="Enter your First Name" value="" class="form-control login-field">
<i class="fa fa-user login-field-icon"></i>
</div>
<div class="form-group">
<input required type="text" id="last_name" placeholder="Enter your Last Name" value="" class="form-control login-field">
<i class="fa fa-user login-field-icon"></i>
</div>
<div class="form-group">
<input required type="text" id="reg_username" placeholder="Enter your User Name " value="" class="form-control login-field">
<i class="fa fa-user login-field-icon"></i>
</div>
<div class="form-group">
<input required type="email" id="email" placeholder="Enter your Email" value="" class="form-control login-field">
<i class="fa fa-user login-field-icon"></i>
</div>
<div class="form-group">
<input required type="password" id="password" placeholder="Password" value="" class="form-control login-field">
<i class="fa fa-lock login-field-icon"></i>
</div>
<a id="register" class="btn btn-success modal-login-btn">Sign up</button>
这里是ajax代码
<script type="text/javascript">
$(document).ready(function(){
$("#register").click(function(e){
if($("form").valid()){
e.preventDefault();
}
var firstname=$('#First_name').val();
var lastname=$('#last_name').val();
var username=$('#reg_username').val();
var password=$('#password').val();
var email=$('#email').val();
$.ajax({
type: "POST",
url: "doRegister.php",
data:
"first_name=" +firstname+ "&last_name=" +lastname+ "&username="+ username+ "&password="+ password+ "&email="+ email ,
success: function(html){
document.write(html);
}
});
return false;
});
});
</script>
我只想用ajax运行必要的字段验证我找不到任何解决方案。
答案 0 :(得分:8)
您需要使用submit
事件而不是click
才能使用html5验证。成功验证后,仅提交事件。
$(document).ready(function() {
$("#register").submit(function(e) {
//---------------^---------------
e.preventDefault();
var firstname = $('#First_name').val(),
lastname = $('#last_name').val(),
username = $('#reg_username').val(),
password = $('#password').val(),
email = $('#email').val();
$.ajax({
type: "POST",
url: "doRegister.php",
data: "first_name=" + firstname + "&last_name=" + lastname + "&username=" + username + "&password=" + password + "&email=" + email,
success: function(html) {
console.log(html);
}
});
return false;
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form action="" method="POST" id="register" role="form">
<!--- ---^---- ------>
<div class="form-group">
<input required type="text" id="First_name" placeholder="Enter your First Name" value="" class="form-control login-field">
<i class="fa fa-user login-field-icon"></i>
</div>
<div class="form-group">
<input required type="text" id="last_name" placeholder="Enter your Last Name" value="" class="form-control login-field">
<i class="fa fa-user login-field-icon"></i>
</div>
<div class="form-group">
<input required type="text" id="reg_username" placeholder="Enter your User Name " value="" class="form-control login-field">
<i class="fa fa-user login-field-icon"></i>
</div>
<div class="form-group">
<input required type="email" id="email" placeholder="Enter your Email" value="" class="form-control login-field">
<i class="fa fa-user login-field-icon"></i>
</div>
<div class="form-group">
<input required type="password" id="password" placeholder="Password" value="" class="form-control login-field">
<i class="fa fa-lock login-field-icon"></i>
</div>
<button type="submit" class="btn btn-success modal-login-btn">Sign up</button>
</form>
&#13;
答案 1 :(得分:1)
如果您的表单无效,则需要退回该功能 - e.preventDefault()
不足以阻止执行ajax
if($("form").valid()){
e.preventDefault();
return; // <-- here
}