如何使用户重定向仅当您输入正确的单词<输入>

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


哔哔one
浏览 97回答 2
2回答

慕斯王

您必须在表单提交按钮上附加事件侦听器,在事件回调中直接检查值而不使用函数(在您的上下文中不需要它),请注意,在if语句中使用符号是错误的,您只使用逻辑运算符&&:+document.getElementById('redirect_form').addEventListener('submit', function(e) {&nbsp; e.preventDefault();&nbsp; var slim = document.getElementById("slim");&nbsp; var shady = document.getElementById("shady");&nbsp; var standup = document.getElementById("standup");&nbsp; if ( standup.value === "stand up" &&&nbsp; &nbsp; &nbsp; &nbsp;shady.value === "shady" &&&nbsp; &nbsp; &nbsp; &nbsp;slim.value === "slim"&nbsp; ) {&nbsp; &nbsp; console.log('All equal');&nbsp; }&nbsp; else {&nbsp; &nbsp; console.log('Not All equal');&nbsp; }&nbsp;&nbsp;&nbsp; setTimeout(function(){&nbsp; &nbsp; window.location = "https://example.com";&nbsp; }, 1000)})<form id="redirect_form" action="/eyerepeat" method="get">&nbsp; <label for="fname">First name:</label>&nbsp; <input type="text" id="slim" /><br /><br />&nbsp; <label for="lname">Last name:</label>&nbsp; <input type="text" id="shady" /><br /><br />&nbsp; <label for="action">Action:</label>&nbsp; <input type="text" id="standup" /><br /><br />&nbsp; <input id="submit" type="submit" value="Submit" /></form>

幕布斯7119047

您的函数需要一个值:returnfunction tree1() {&nbsp; return slim.value === "slim";}function tree2() {&nbsp; return shady.value === "shady";}function tree3() {&nbsp; return standup.value === "stand up";}要确定这三者是否都是 ,请使用 ,而不是:true&&+if (tree1() && tree2() && tree3() ) {&nbsp; window.location = "https://example.com";}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript