javascript - 仅当您输入正确的单词时如何使用户重定向
问题描述
我正在制作一个有 3 个输入的网站。每个输入,我希望仅当用户为所有 3 个输入输入正确的单词时才重定向用户。如果用户提交的内容均不正确,则什么也不做。这是与我上一篇类似的帖子,但它只有 1 个输入,这非常简单。我不是这方面的大师,所以任何反馈或解决方案都会很棒。
var slim = document.getElementById("slim");
var shady = document.getElementById("shady");
var standup = document.getElementById("standup");
function tree1() {
slim.value === "slim";
}
function tree2() {
shady.value === "shady";
}
function tree3() {
standup.value === "stand up";
}
if (tree1() + tree2() + tree3() === true) {
window.location = "https://example.com";
}
<form action="/eyerepeat" method="get">
<label for="fname">First name:</label>
<input type="text" id="slim" /><br /><br />
<label for="lname">Last name:</label>
<input type="text" id="shady" /><br /><br />
<label for="action">Action:</label>
<input type="text" id="standup" /><br /><br />
<input id="submit" type="submit" value="Submit" />
</form>
解决方案
您的函数需要return
一个值:
function tree1() {
return slim.value === "slim";
}
function tree2() {
return shady.value === "shady";
}
function tree3() {
return standup.value === "stand up";
}
要确定这三个是否都是true
,请使用&&
,而不是+
:
if (tree1() && tree2() && tree3() ) {
window.location = "https://example.com";
}
推荐阅读
- artifactory - Conda 远程存储库 URL 是否与 Artifactory 一起使用?
- sql - SQL 代码在 MS-Access 中进行数据联合并保留相关信息
- python - 电子前沿基金会的猫笑话
- c++ - 为什么我的 cpp 单元测试在运行时寻找额外的库?
- python - 如何在 django 命令中实现应用程序命名空间?
- mongodb - 在 MongoDB 中重构文档
- angularjs - 如何编写一个 angularjs 指令来增加渲染该元素的延迟?
- java - 如何在 Java 中正确地将值添加到 3D 数组
- c# - 我正在使用 C# 对我的角色进行运动和破折号编码,但是当我为破折号添加冷却时间时,它搞砸了
- php - “调用未定义的函数 getOption()”