2016-10-05 74 views
0

我摆弄着假装“登录屏幕”,但在重定向页面时遇到麻烦。Javascript:重定向页面onclick

我已经尝试了几个我在网站上发现的不同建议,但到目前为止没有任何工作。

我真的认为我只是做错了。

有什么建议吗?

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我已经试过(在同一地区为document.location.href

$(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'); 
      } 
     }); 
    }); 

代码

  1. document.location
  2. window.location
  3. window.location.href
  4. location.href
+0

您能在JSFiddle重现这个问题吗? –

+0

@YeldarKurmangaliyev不幸的是,虽然它在Chrome中工作正常,但它在JSFiddle中完全不起作用。我得到“{”错误“:”请使用POST请求“}”。但是,无论如何,如果它有帮助,我可以创造一个吗? – Asteria

回答

3

默认情况下,button的作用就像input type="submit",所以它试图提交表单。使用event.preventDefault()来避免这种行为。

正确的重定向方式是设置window.location.href

此外,我强烈建议不要尝试客户端认证,因为任何人都可以在您的js文件中读取登录名和密码。

+0

啊,那是我出错的地方。谢谢! 是的,当我提出这个问题时,它只是一个“假装”登录页面。我没有建立真正的知识(然而:P),再加上这仅仅是为了个人的乐趣和学习。不是WWW! – Asteria