0

ユーザーが情報を入力して送信できるWebフォームがあります。送信が成功するとページに移動するような動作が必要です。ただし、フォーム送信エラーが発生した場合 (私の場合は既存の 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では、送信が成功した場合に次のページ ( ) にリダイレクトするように修正するにはどうすればよいでしょうか?

4

1 に答える 1

1

次を使用して、成功条件内でリダイレクトできますwindow.location.href

if (status === "success") {
  window.location.href = 'http://url.com'
}
于 2013-06-18T15:06:58.683 に答える