简体   繁体   English

为什么else语句在if语句之后执行?

[英]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.

相关问题 为什么只执行“else”语句? - Why does only “else” statement execute? 为什么这个if / else语句总是执行if分支? - Why does this if/else statement always execute the if branch? Chrome扩展程序,如果else语句不执行else - Chrome extension if else statement does not execute the else 为什么 if 条件语句在 else if(这是错误的)之后终止,尽管在这个 if 条件语句中有另一个 else if? - Why does if conditional statement terminate after else if(which is false)although there is another else if in this if conditional statement? 为什么 if else 语句与 else if 语句的工作方式不同? - Why does if else statement not work the same way as else if statement? 为什么我的代码执行return语句后定义的函数? - Why does my code execute a function defined after the return statement? 为什么此代码似乎在return语句之后执行? - Why does this code seem to execute after the return statement? 为什么我的if语句与else if而不是OR运算符一起工作 - Why does my if statement work with an else if, but not an OR operator 为什么 javascript if/else 语句不起作用 - Why does the javascript if/else statement not working 为什么它会立即跳转到我的 else 语句? - Why does it jump to my else statement immediately?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM