[英]Prevent Default is not working
我已經在這里檢查過了,這些解決方案都無法解決我的問題。 我似乎無法使preventDefault()起作用。 代碼如下:
jQuery的:
$("#changepassword").submit(function(event) {
event.preventDefault(); // stop normal form submission
$.ajax({
url: "changePassword.php",
type: "POST",
data: $(this).serialize(), // you also need to send the form data
dataType: "html",
success: function(html) {
$("#s-window").append(html);
}
});
});
HTML:
<div id="s-window">
<form id="changepassword" action="changePassword.php" method="POST">
<input type="password" name="currentPassword" placeholder="Current Password"/>
<input type="password" name="newPassword" placeholder="New Password"/>
<input type="password" name="confirmPassword" placeholder="Confirm Password"/>
<input class="button" type="submit" value="Change Password" />
</form>
</div>
我的上司沒有任何錯誤。 changePassword.php可以正常工作。 但是,與其更新我的#s窗口,不如將其重定向到changePassword.php頁面。
如何重新編碼以使所有內容在頁面上發生?
您可以使用return false;
來嘗試這種方式return false;
,如果您有任何困惑,請參見此處更多event.preventDefault()與return false
$("#changepassword").submit(function(event) {
//event.preventDefault(); // stop normal form submission
return false;
});
您需要等待文檔准備就緒:
$(document).ready(function(){
$("#changepassword").submit(function(event) {
event.preventDefault(); // stop normal form submission
$.ajax({
url: "changePassword2.php",
type: "POST",
data: $(this).serialize(), // you also need to send the form data
dataType: "html",
success: function(html) {
$("#s-window").append(html);
}
});
});
});
在您的ajax調用中,將dataType: "html"
更改為dataType: "script"
至少在IE中, event
是全局變量(根據“事件”是否在JavaScript中是保留字? )。 這可能不是造成問題的原因,但是仍然可以更改命名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.