我有一个网络表单,让用户输入一些信息并提交。我想要这样的行为,如果提交成功,它将转到页面。但是如果有一些表单提交错误(在我的情况下是由于现有的ID),我希望表单保持在那里而不进入下一页并提醒用户“ID已经存在”。我使用jQuery来处理成功和失败事件:
function checkForm() {
$.post("/myapp/rest/customer/created", $("form").serialize(), function(
data, status) { })
.done(function() {alert("post success");})
.fail(function() {alert("post error");});
return false;
}
我的表格是:
<form action="/myapp/rest/customer/created"
onsubmit="return checkForm();" method="POST">
<table border="1">
<tr>
<td>Customer name:</td>
<td><input type="text" id="name" name="name"></td>
</tr>
<tr>
<td>Customer ID:</td>
<td><input type="text" id="id" name="id"></td>
</tr>
<tr>
<td>Customer DOB:</td>
<td><input type="text" id="dob" name="dob"></td>
</tr>
</table>
<br /> <input type="submit" value="Submit">
</form>
提交后,无论提交是成功还是失败,我只会看到弹出消息框中的“发布成功”或“发布错误”。但如果成功的话,它永远不会进入下一页。我甚至尝试了一种不同的方法,如下面的代码,但它也不起作用:
function checkForm() {
$.post("/myapp/rest/customer/created", function(data, status) {
if (status === "success") {
alert("post success");
} else {
alert("post error");
}
});
return false;
}
那么如果提交成功,如何更正它以使其重定向到下一页(/myapp/rest/customer/created
)?
答案 0 :(得分:1)
您可以使用window.location.href
if (status === "success") {
window.location.href = 'http://url.com'
}