javascript - 如何在 3 次尝试使用 javascript 后禁用按钮?
问题描述
3 次尝试后如何禁用提交按钮?这就是所有代码(感谢DCR获得了一些关于 javascript 的帮助)
/*This Script allows people to enter by using a form that asks for a
UserID and Password*/
function pasuser(form) {
if (form.identifier.value=="GG") {
if (form.pass.value=="123") {
window.location('https://www.google.com/');
} else {
alert("Invalid Password");
}
} else { alert("Invalid UserID");
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="/style.css">
<script src="/server.js"></script>
<title></title>
</head>
<body>
<div class="box">
<h2>Login</h2>
<form>
<div class="inputBox">
<input type="text" name="identifier" required="">
<label>Username</label>
</div>
<div class="inputBox">
<input type="password" name="pass" required="">
<label>Password</label>
</div>
<input type="button" value="Submit" onclick="pasuser(form)">
</form>
</div>
</body>
</html>
解决方案
var trieds = 0;
function pasuser(form) {
if (form.identifier.value=="GG") {
if (form.pass.value=="123") {
window.location = 'https://www.google.com/';
} else {
alert("Invalid Password");
trieds += 1;
}
} else {
alert("Invalid UserID");
trieds += 1;
}
//Change <input type='button'> for <button>
document.querySelector("button").disabled = (trieds === 3) ? true : false;
}
推荐阅读
- python - python - 如何在Python中加入SQLAlchemy中的两个表时在一个对象中获取键值对?
- python - 将具有不同列的多个数据框合并在一起,除了 5
- r - 训练 SVM 模型时出错:错误:结果中的一个或多个因子级别没有数据:'2'
- python - 熊猫垂直拆分列
- ida - Open Modelica 中 IDA 求解器的最大时间步长
- php - 使用 PHP 填充下拉列表的问题
- angular - 使用带有 Angular 的 MapBox GL JS - 错误:无效类型:“容器”必须是字符串或 HTMLElement
- flutter - 在颤动中从 webview 导航到 InApp 屏幕
- html - 如何将文本或图像与 HTML 或 CSS 中的数值对齐?
- pdf - 链接以从 Markdown 文档加载本地 PDF 的特定页面