由于某种原因,我的jquery验证和ajax将无法正常工作,我无法看到我在哪里编写此脚本错误。我在我的标题中引用了jquery,当我删除ajax函数时,脚本运行正常。有人看到最新情况吗?
<script type="text/JavaScript">
$(document).ready(function() {
$('#btn-submit').click(function(e) {
// stop normal button action, whatever it is
e.preventDefault();
$(".error").hide();
var hasError = false;
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var emailaddressVal = $("#UserEmail").val();
if(emailaddressVal == '') {
$("#submit").after('<span class="error">Please enter a email address.</span>');
hasError = true;
}
else if(!emailReg.test(emailaddressVal)) {
$("#submit").after('<span class="error">Email Not Valid');
hasError = true;
} else if(emailReg.test(emailaddressVal)){
$("#submit").after('<span class="error">Email Updated');
}
if (!hasError) {
$.ajax({
type: "POST",
url: "updateEmails.php",
data: "UserEmail="+emailaddressVal,
success: function(data){
$(".parentEmail").html(emailaddressVal);
}
});
}
});
});
</script>
<form method="post" name="form1" action="">
<label>Email Address:</label>
<input type="text" name="UserEmail" id="UserEmail" value="" size="32" />
<input type="submit" value="Submit" id="btn-submit" />
</form>
答案 0 :(得分:0)
它正在验证,但是,您在ID为“submit”的元素后显示错误消息,而您的提交按钮的ID为“btn-submit”。
你需要修改它:
$("#submit").after(...
到此:
$("#btn-submit").after(...
还在Alastair Pitts提到的html中添加结束范围标记