首页 > 解决方案 > 仅当您输入正确的单词时如何使用户重定向

问题描述

我正在制作一个有 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>

标签: javascripthtmlinput

解决方案


您的函数需要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";
}

推荐阅读