首页 > 解决方案 > Html 页面无法加载(Javascript)

问题描述

我一直在尝试将页面从一个登录页面切换到另一个。它似乎没有加载或者我做错了什么?

代码笔链接:https ://codepen.io/batajusasmit/pen/OJVyGEW

这是我的代码。

 function validate() {
     var username = document.getElementById('username').value;
     var password = document.getElementById('password').value;

         if (username == 'admin' && password == 'admin') {
             alert('login successful!');
             window.location = 'link.html';
             return false;
            }
          }

标签: javascriptredirectwindow.location

解决方案


您必须停止在同一页面上提交,然后转到新位置

您已经使用validate()了 button 中的功能onclick。但是你应该使用return validate()which 将返回布尔值来阻止表单提交

function validate() {
 var username = document.getElementById('username').value;
 var password = document.getElementById('password').value;

     if (username == 'admin' && password == 'admin') {
         alert('login successful!');
         window.location = '/'; //change with your location
         return false;
        }
      }
<div class="container">
<form>
    <h1 id="form">Login!</h1>
    <div class="login">
        <label class="name">username:</label>
        <input id="username" class="username" type="text" placeholder="name...">

        <label class="pass">password:</label>
        <input id="password" class="password" type="password" placeholder="password...">

    </div>

    <div class="submit">
        <button id="submit" class="btn btn-primary" onclick="return validate()">Submit</button>
    </div>


   </form>
 </div>


推荐阅读