javascript - 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;
}
}
解决方案
您必须停止在同一页面上提交,然后转到新位置
您已经使用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>
推荐阅读
- r - grDevices::dev.new() 第一次不起作用
- vue.js - 带有 imagemin webpack 插件的 Vue cli 3
- sql - 大于或小于一个数量
- ms-access - 通过访问表单上的按钮将费用记录复制到发票详细信息
- java - Spring Integration 从 REST 服务获取分页结果
- javascript - 查找最大匹配计数
- r - 使用 ggplot scale_x_yearmon 缺少 yearmon 标签
- sql - SSIS 中的 Google Analytics/Adwords 数据?
- dataframe - 为什么 Dask 在我的 Dataframe 中填写“foo”和 1
- javascript - 如果您只知道属性名称,您能否通过 setter 和 getter 从对象的列表中定义属性?