简体   繁体   English

如何在 javascript 中使用带有多个下拉列表的 if else 语句?

[英]How to use if else statment in javascript with multiple drop-down list?

I am trying to print select in multiple drop-down list, but prints only two words(CHENNAI AND IOS-XE, BANGALORE AND IOS-XR) could not print the remaining words Can anyone help me fix this?我正在尝试在多个下拉列表中打印 select,但仅打印两个单词(CHENNAI 和 IOS-XE、BANGALORE 和 IOS-XR)无法打印剩余的单词有人可以帮我解决这个问题吗?

Required Output:所需 Output:

if i select CHENNAI+IOS-XE ---> output: CHENNAI AND IOS-XE如果我 select 钦奈+IOS-XE ---> output:钦奈和 IOS-XE

else if BANGALORE+IOS-XR ---> output: BANGALORE AND IOS-XR否则,如果班加罗尔+IOS-XR ---> output:班加罗尔和 IOS-XR

else if CHENNAI+IOS-XR ---> output: CHENNAI AND IOS-XR否则如果 CHENNAI+IOS-XR ---> output: CHENNAI 和 IOS-XR

else if BANGALORE+IOS-XE ---> output: BANGALORE AND IOS-XE否则如果 BANGALORE+IOS-XE ---> output: BANGALORE 和 IOS-XE

<html>
<body>
  <p>SELECT THE REGION </p>
  <select id="choose">
    <option value="a1">CHENNAI</option>
    <option value="b1">BANGALORE</option>
  </select></p>
  
  <p>SELECT THE ROUTER TYPE </p>
  <select id="choose1">
    <option value="a2">IOS-XE</option>
    <option value="b2">IOS-XR</option>
  </select></p>
  
  <button onclick="button()">CLICK</button>

  <script>
    function button() {
      var x = document.getElementById("choose").value;
      var y = document.getElementById("choose1").value;
      if (x == 'a1' || y == 'a2' ) {
        document.write("CHENNAI AND IOS-XE");
      }
      else if (x == 'b1' || y == 'b2'){
        document.write("BANGALORE AND IOS-XR");
      }
      else if (x == 'a1' || y == 'b2'){
        document.write("CHENNAI AND IOS-XR");
      }   
      else if (x == 'b1' || y == 'a2'){
        document.write("BANGALORE AND IOS-XE");
      }       
    }
  </script>
</body>
</html>

Your else statement cannot contain a condition check.您的else语句不能包含条件检查。

You need to change the logic to the following:您需要将逻辑更改为以下内容:

if (x == 'a1') {
  document.write("A SELECTED");
}
else if (x == 'b1'){
//   ^  added this :)
  document.write("B SELECTED");
}

else shouldn't have any conditions in it, if you want to use a condition for it you should use else if : else里面不应该有任何条件,如果你想为它使用条件,你应该使用else if

else if ( x == 'b1' ) {
    // Your statement
} 

or you can use a general else for all other cases:或者您可以在所有其他情况下使用一般else

else {
    // Your statement
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM