[英]location.href is not working in if sentence in javascript
我想用密码简化页面设置方式。
在javascript中,当我使用任何带有“位置”的代码(我尝试了所有操作。.替换,分配...等)时,它什么都没有用!
但是当我使用window.open()而不是location.href时,它可以正常工作。 但我想保持原样...不是新标签页或新窗口...
帮我...
在HTML
<form action="" method="post" name="Please enter the password to continue.">
<div class="WorkPassword">
<input type="password" class="button" id="WorkInputPassword"
name="password" placeholder="Please enter the password"/>
<input type="submit" class="button" id="submit" value="Enter" onClick="goLogin();">
</div>
和在javascript中。
var input = document.getElementById( 'WorkInputPassword' );
var goLogin = function () {
var password = input.value;
if (password === '1234') {
location.href="google.com";
return false;
} else {
alert( 'check again' );
input.value = '';
return false;
}
};
如果要重定向到新域,则应使用完整的地址(包括http://
或https://
)。
location.href="http://google.com";
然而,在你的情况下,它是不够的,因为你是一个内submit
表单的事件,如果你想取消submit
事件必须使用event.preventDefault()
函数中。
因此,应该是这样的:
if (password === '1234') {
event.preventDefault();
location.href="http://google.com";
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.