我試圖在成功登錄後直接指向另一個文件。但是,在登錄成功警告顯示後,似乎沒有能夠轉到b.html。它似乎仍然在同一頁面。我曾嘗試document.location.href
,window.location.replace
但它似乎沒有工作jQuery重定向到另一個文件
<script>
window.onload = function() {
document.addEventListener("deviceready", init, false);
}
function init() {
document.getElementById("btnSave").addEventListener("click", saveData, false);
document.getElementById("btnGet").addEventListener("click", getData, false);
}
function getData() {
var email1 = document.getElementById("email1").value;
var pass1 = document.getElementById("password1").value;
var email2 = window.localStorage.getItem("email");
var pass2 = window.localStorage.getItem("pass");
if (email1 == email2 && pass1 == pass2) {
alert("Login successfully");
window.location.href = "/b.html";
} else {
alert("Invalid login credentials");
}
}
</script>
<form class="login-form">
<input type="email" id="email1" name="email1" placeholder="Email Address" required/>
<input type="password" id="password1" name="password1" placeholder="Password" required/>
<button id="btnGet" type="submit" name="submit">Login</button>
</form>
使用完整的URL與域和檢查。 http://stackoverflow.com/questions/6109527/window-location-href-not-working-in-form-onsubmit – Cruzer
1.不要在表單中調用任何'name =「submit」'。 2.如果提交表單,則離開頁面,腳本可能沒有時間執行或表單不會向服務器發送任何內容。而是讓表單動作重定向。 3.在location.href中使用完全限定的URL - PS:如果需要在處理程序中執行JavaScript,請將按鈕更改爲鍵入=「按鈕」 - otherise在服務器上執行操作或取消提交 – mplungjan
但如果我有表單動作重定向,即使它的失敗登錄,它仍然會將用戶引導到該頁面? – fypforstack