取消表单提交后,windows .location.href仍然不能工作
window.location.href still not working after cancelling form submission
我有一个登录表单
<div id='login_form'>
<table>
<tr>
<td>Username:</td>
<td><input size=25 type='text' id='username'></td>
</tr>
<tr>
<td>Password:</td>
<td><input size=25 type='password' id='password'></td>
</tr>
<tr>
<td colspan='2' align='right'>
<input type='button' id='login' value='Login' onkeydown='normalobjKeyFunc(this)' onfocus='itemFocus(this)' onblur='itemBlur(this)'/>
</td>
</tr>
</table>
</div>
然后是javaScript
var input_username = document.getElementById("username").value;
var input_password = document.getElementById("password").value;
if(input_username === "123" && input_password === "456"){
alert("Correct username & password");
window.location.href="../../result.html";
return false;
}
但是当我收到"正确的用户名&,页面没有重定向到result.html。我检查了"../../result。html"存在。
- 我发现有些人说应该取消提交,所以我删除了表单,并将登录按钮的"type = "submit"更改为"type = "button",但它不起作用。
- 还有另一种方法,在"window.location.href="../../result.html" "之后添加"return false",但它不能正常工作。
有人知道吗????提前感谢!
用户名/密码检查应该在后端 (Java, PHP, Node, .NET)
你的表单应该在表单标签中。检查应该在onsubmit回调中完成:
<script>
function authenticate(){
var input_username = document.getElementById("username").value;
var input_password = document.getElementById("password").value;
if(input_username==="123"&&input_password==="456"){
alert("Correct username & password");
window.location.href = "../../result.html";
}
else {
// Error
}
return false;
}
</script>
<form onsubmit="authenticate()">
<table>
<tr>
<td>Username:</td>
<td><input size="25" type="text" id="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input size="25" type="password" id="password"></td>
</tr>
<tr>
<td colspan="2" style="text-align: right">
<button onkeydown="normalobjKeyFunc(this)" onfocus="itemFocus(this)" onblur="itemBlur(this)">Login</button>
</td>
</tr>
</table>
</form>
在你的按钮中添加一个onclick来调用javascript函数。
<input type='button' id='login' onclick="authenticate()" value='Login' onkeydown='normalobjKeyFunc(this)' onfocus='itemFocus(this)' onblur='itemBlur(this)'/>
相关文章:
- 用javascript将script元素附加到头部;铬不能工作
- 为什么jQuery代码段在没有IFrame的情况下可以工作,而在有IFrame时却不能工作
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作
- 引导程序's的javascript在本地工作,但在部署到服务器时不能工作
- JQuery脚本没有'Don’我不能工作两次
- "npm运行构建:css"不能工作,而当我自己运行脚本时是可以的
- ng样式在与的预标记中!重要的是不能工作
- angularjs 控制器在指令中使用控制器作为不能工作
- jQuery LightSlider不能工作,但在JSFiddle中工作得很好
- Javascript - 我如何让一个语句工作一次,如果它已经工作了 1 次,则不能工作,然后再次工作
- 我的查询获胜'因为一个剧本,它不能工作(它以错误的方式工作)!?但为什么呢?SESSIONS和PHP
- 我的Javascript代码没有'铬不能工作
- 使用JQuery调整图像大小和重新排列图像;第一次装载时不能工作
- 为什么不是'这个Javascript计算器不能工作
- Javascript在窗口中返回.onbeforeunload不会'铬不能工作
- Javascript代码包含在html中时可以工作,但单独加载/注入时则不能工作
- 简单的jquery选择器不会;铬不能工作
- 为什么不'这个Javascript函数不能工作
- 为什么不'这个js数组函数不能工作
- 视口比例;方向改变后不能工作