[英]Why does the else statement execute after the if statement?
I have a form with one text input. 我有一个带有一个文本输入的表格。 When I press enter, if the text matches the if statements, then a function runs.
当我按Enter键时,如果文本与if语句匹配,则函数运行。
function command() { var input = document.getElementById("url").value; if (input == "/help"){ alert("hi"); help(); } if (input == "/reset"){ reset(); } if (input == "/custom"){ customSetting(); } if (input == "/contact"){ modal1.style.display = "block"; } if (input == "/docs"){ googleDocs(); } else if (input == "/classroom"){ googleClassroom(); } else { alert("This command doesn't exist."); } } $(document).ready(function () { $('#command').attr('action', 'javascript:void(0);'); });
<form onsubmit="command()" id="command"> <input type="text" id="url" class="form1"> <input type="submit" style="visibility: hidden;"> </form>
The problem is that the else statement runs even if the if statement is true. 问题是即使if语句为true,else语句也会运行。 Thanks in advance.
提前致谢。
You should use else if
: 如果出现以下情况,则应使用
else if
:
function command() { var input = document.getElementById("url").value; if (input == "/help") { alert("hi"); help(); } else if (input == "/reset") { reset(); } else if (input == "/custom") { customSetting(); } else if (input == "/contact") { modal1.style.display = "block"; } else if (input == "/docs") { googleDocs(); } else if (input == "/classroom") { googleClassroom(); } else { alert("This command doesn't exist."); } } $(document).ready(function() { $('#command').attr('action', 'javascript:void(0);'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form onsubmit="command()" id="command"> <input type="text" id="url" class="form1"> <input type="submit" style="visibility: hidden;"> </form>
Alternatively, consider using switch
: 或者,考虑使用
switch
:
function command() { var input = document.getElementById("url").value; switch (input) { case "/help": alert("hi"); help(); break; case "/reset": reset(); break; case "/custom": customSetting(); break; case "/contact": modal1.style.display = "block"; break; case "/docs": googleDocs(); break; case "/classroom": googleClassroom(); break; default: alert("This command doesn't exist."); } } $(document).ready(function() { $('#command').attr('action', 'javascript:void(0);'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form onsubmit="command()" id="command"> <input type="text" id="url" class="form1"> <input type="submit" style="visibility: hidden;"> </form>
it works but the text submitted needs to have a "/" in front of it to work, you can concatenate a "/" to your input, or just check for the word without the "/". 它可以工作,但是提交的文本前面必须有一个“ /”才能起作用,您可以将“ /”连接到输入中,或者仅检查不带“ /”的单词。
For example, you could use: 例如,您可以使用:
var input = '/' + document.getElementById("url").value;
or: 要么:
if (input == "help"){
alert("hi");
help();
}
if (input == "reset"){
reset();
}
if (input == "custom"){
customSetting();
}
if (input == "contact"){
modal1.style.display = "block";
}
if (input == "docs"){
googleDocs();
}
else if (input == "classroom"){
googleClassroom();
}
else {
alert("This command doesn't exist.");
}
You may find it useful to use a switch with so many cases. 您可能会发现在许多情况下使用开关很有用。
default
is hit if all other cases are false, which I think was was your intended logic for the else
block. 如果所有其他情况均为假,则
default
为hit,我认为这是您为else
块准备的逻辑。
function command() { var input = document.getElementById("url").value; switch (input) { case "/help": return alert("hi"); case "/reset": return alert("reset"); default: return alert("This command doesn't exist."); } } $(document).ready(function() { $('#command').attr('action', 'javascript:void(0);'); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form onsubmit="command()" id="command"> <input type="text" id="url" class="form1"> <input type="submit" style="visibility: hidden;"> </form>
There must be only one 'if' for every single case related conditions 每个与个案相关的条件都必须只有一个“如果”
function command() {
var input = document.getElementById("url").value;
if (input == "/help"){
alert("hi");
help();
}
else if (input == "/reset"){
reset();
}
else if (input == "/custom"){
customSetting();
}
else if (input == "/contact"){
modal1.style.display = "block";
}
else if (
input == "/docs"){
googleDocs();
}
else if (input == "/classroom"){
googleClassroom();
}
else {
alert("This command doesn't exist.");
}
}
$(document).ready(function () {
$(‘#command').attr('action', ‘javascript:void(0);');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.