【发布时间】:2015-10-16 15:36:16
【问题描述】:
我正在构建一个假的登录表单,如果用户插入正确的凭据,它应该在本地加载不同的 url。这是html 代码:
<div class="container">
<h2>Login page - Welcome</h2>
<form id="loginForm" onsubmit="subLogin()">
Username: <input id="userName" type="text" name="userName" required><br/>
Password: <input id="passWord" type="password" name="password" required><br/>
<button type="submit" id="login-button" value="Login">Login</button>
</form>
</div>
这是javascript,我尝试通过window.location.href = "/index.html"重新定位用户。
function subLogin() {
var userName = document.getElementById('userName').value;
var passWord = document.getElementById('passWord').value;
if (userName !== 'mickeymouse' || passWord !== 'DisneyLand') {
alert('Your username or password is not correct');
} else {
window.location.href = 'http://localhost:8000/index.html';
};
};
如果用户名或密码错误,但如果正确,window.location 将无法正常工作。
有没有办法用纯javascript 解决这个问题,还是通过ajax 使用XMLHttp GET Request 更好?提前感谢您的回复!
【问题讨论】:
-
不明白,你的问题出在哪里。我的意思是哪个没有按预期工作
-
我忘了插入那部分,我的错。我对
window.location有疑问,因为如果正确提交了用户名和密码,它不会将用户重定向到'/index.html' -
重定向页面是通过 (window.location=url) 完成的! window.location.href 只返回当前 url w3schools.com/js/js_window_location.asp
-
我也试过
window.location,但它不起作用。
标签: javascript html forms xmlhttprequest