我想通過ajax執行php文件,但它只是像正常一樣通過表單加載並加載一個新頁面。我在這裏檢查了大量的代碼,仍然不會通過submitHandler和ajax執行。我已經檢查主機提供商和ajax支持,我已經做了jQuery的其他測試,它工作正常。只是不能讓它工作。 PHP文件執行perferectly並插入數據到數據庫中,所以我猜測我錯過了驗證代碼的東西。Ajax submitHandler沒有執行
$("#emailForm").validate({
submitHandler: function(form) {
// event.preventDefault();
$.ajax({
url: $(form).attr("action"),
type: $(form).attr("method"),
data: $(form).serialize(),
success: function(response) {
document.getElementById("fee").style.display = "block";
}
});
return false;
}
});
#fee {
display: none;
}
<html>
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.min.js"></script>
</head>
<body>
<div class="screen">
<div class="contents">
<form class="cmxform" id="emailForm" method="post" action="process.php">
<fieldset>
<legend>Sign up now and secure your username!
</legend>
<p>
<label for="cname">Username (required, at least 5 characters)
</label>
<input id="cname" name="name" minlength="5" type="text" required>
</p>
<p>
<label for="cpassword">Password (required, at least 8 characters)
</label>
<input id="cpassword" name="password" minlength="8" type="password" required>
</p>
<p>
<label for="cemail">Email (required)
</label>
<input id="cemail" type="email" name="email" required>
</p>
<p>
</p>
<p>
<input class="submit" type="submit" value="Submit">
</p>
</fieldset>
</form>
<div id='fee'>Signup Complete!
</div>
</div>
</div>
</body>
</html>
1)你確定你的JavaScript是執行呢? 2)你檢查了控制檯是否有錯誤? 3)我沒有看到你阻止表單提交('e.preventDefault()') –
ohhh!我必須防止默認?我會谷歌:) – drume
@JasonP我更新了我的JavaScript,但它仍然加載在新的頁面,我正在使用preventDefault正確? – drume