【发布时间】:2015-04-15 17:03:06
【问题描述】:
我已经检查了这里,这里的这些解决方案都没有解决我的问题。 我似乎无法让 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-window,而是将我重定向到 changePassword.php 页面
如何重新编码以使页面上的所有内容都发生?
【问题讨论】:
-
如果您仍然使用 ajax,为什么不删除
form属性。 -
主要是因为我不知道自己在做什么。如果可行,我可以这样做吗?你有什么可以解释我为什么要做出这些改变的吗?
-
你为什么使用 dataType: "html"。只需将其更改为 dataType: "script it will work"。我在下面更新了我的答案。
-
如果在加载 dom 之前运行代码将表单与 submit 事件绑定,则代码不会执行任何操作。
标签: javascript jquery preventdefault