javascript - 我如何制作一个 js 代码来重定向到具有特定输入的特定 html 页面?
问题描述
让我更好地解释一下,我想知道如何创建一个 js 代码来检查 html 输入是否正确,以防它会将您重定向到另一个页面,这是我根据我设法做到的尝试查出。
html部分:
<form name="access" onsubmit="return validate()">
<input
type="text"
id="inputbox"
value="Password"
pattern="idkwhatishoouldwriteinhere"
/>
<input type="submit" value="Submit" />
</form>
js部分:
function validate() {
if (document.access.Password.value != "idkwhatishoouldwriteinhere") {
alert("Wrong password");
document.access.Password.focus();
return false;
} else {
window.open("index.html");
}
}
如果你想知道为什么我把“答案”放在模式中是因为这应该是一个小彩蛋,我觉得直接看 js 是没有意义的,因为它包含你应该重定向到的链接。在此处输入代码
解决方案
您需要为您的输入提供 name Password
,否则document.access.Password
未定义。
function validate() {
if (document.access.Password.value != "idkwhatishoouldwriteinhere") {
alert("Wrong password");
document.access.Password.focus();
return false;
} else {
window.open("index.html")
}
}
<form name="access" onsubmit="return validate()">
<input type="text" id="inputbox" value="Password" name="Password" />
<input type="submit" value="Submit" />
</form>
<!-- password is "idkwhatishoouldwriteinhere" -->
推荐阅读
- postgresql - 将来自 web 的 tsv 文件中的数据插入到 postgres 表中
- django - 如何在 django admin 中使用 multselect 字段进行过滤
- swift - UIStackView 内的项目溢出
- php - 将匹配词提取到 JSON 文件
- javascript - 激活和停用 Marketing Assets Dynamics 365
- git - 如何将 TornadoFX 的某个分支添加到我的项目中?
- discord - 如何将超链接嵌入到语音频道?
- reactjs - 如何修复参考错误:使用反应找不到变量触摸事件?
- javascript - 使用 javascript,改变锯齿状数组中元素长度的有效方法是什么?
- python - 如何在没有 Keras 的情况下使用 TensorFlow 2