我已经检查过这里,这里的这些解决方案都没有解决我的问题。 我似乎无法让 preventDefault() 工作。代码如下:
J查询:
$("#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-window,而是将我重定向到 changePassword.php 页面
我如何重新编码才能使页面上的一切都发生?
最佳答案
您需要等到文档准备好:
$(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);
}
});
});
});
关于javascript - 防止默认不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29656395/