[英]Javascript: Redirect page onclick
I'm fiddling around with a pretend "Logon screen" but am having trouble when it comes to redirecting the page.我正在摆弄一个假装的“登录屏幕”,但在重定向页面时遇到了麻烦。
I've tried a couple of different suggestions that I've found on the site, but so far nothing has worked.我尝试了在网站上找到的几个不同的建议,但到目前为止没有任何效果。
I honestly think I'm just doing it wrong.老实说,我认为我只是做错了。
Any advice?有什么建议吗?
HTML HTML
<div class="title"><h1>Webby Site</h1></div>
<div class="login-page">
<div class="form">
<form class="login-form">
<input id="User" type="text" placeholder="username"/>
<input id="PWord" type="password" placeholder="password"/>
<button class="button">Login</button>
<p class="message">Not registered? <a>Create an account</a></p>
</form>
</div>
</div>
JS JS
$(document).ready(function(){
$('.button').click(function(){
if ($('input#User').val() === "1" && $('input#PWord').val() === "1"){
document.location.href = "www.yoursite.com";
} else {
alert('Incorrect Account Details');
}
});
});
Code I've tried (in the same area as document.location.href
)我尝试过的代码(与
document.location.href
位于同一区域)
document.location
window.location
window.location.href
location.href
By default, button
acts like input type="submit"
, so it's trying to submit your form.默认情况下,
button
作用类似于input type="submit"
,因此它正在尝试提交您的表单。 Use event.preventDefault()
to avoid this behaviour.使用
event.preventDefault()
来避免这种行为。
Correct way to redirect is setting window.location.href
.重定向的正确方法是设置
window.location.href
。
Also, I strongly recommend not to try client-side authentification, because anyone could just read login and password in your js file.另外,我强烈建议不要尝试客户端身份验证,因为任何人都可以在您的 js 文件中读取登录名和密码。
For HTML, you can use <button onclick="location.href='url'">text</button>
对于 HTML,您可以使用
<button onclick="location.href='url'">text</button>
You can also use <button onclick="window.location.replace(url)">text</button>
你也可以使用
<button onclick="window.location.replace(url)">text</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.