根据下拉选择显示不同的表

时间:2019-07-29 05:12:59

标签: javascript node.js

我有10个不同的表。此类表格如下:

<table id="Vehicle-PS-Software">  
            <tr>
              <td width=10%>
                <input type="checkbox" name="option" value="TestID">
              </td>
              <td width=20%>
                Test ID
              </td>
            </tr>
            <tr>
              <td>
                <input type="checkbox" name="option" value="ServiceNow">
              </td>
              <td>
                ServiceNow
              </td>
            </tr>

            <tr>
              <td>
                <input type="checkbox" name="option" value="GIT">
              </td>
              <td>
                Git Hub
              </td>
            </tr>
            <tr>
              <td>
                <input type="checkbox" name="option" value="Jira">
              </td>
              <td>
                Jira
              </td>
            </tr>
            <tr>
            <tr>
              <td>
                <input type="checkbox" name="option" value="Confluence">
              </td>
              <td>
                Confluence
              </td>
            </tr>
            <tr>
              <td>
                <input type="checkbox" name="option" value="BO">
              </td>
              <td>
                BO Access
              </td>

              <td>
                <input type="file" name="sampleFile" />
              </td>
            </tr>
          </table>

我的下拉菜单如下:

 <div class="dropdown">
         <button onclick="myFunction()" class="dropbtn">Teams</button>
         <p>please select your team</p>
          <div id="myDropdown" class="dropdown-content">
            <a href="#Vehicle PS">Vehicle PS</a>
            <a href="#Vehicle RTB">Vehicle RTB</a>
            <a href="#Property PS">Property PS</a>
            <a href="#Property RTB">Property RTB</a>
            <a href="#Commons PS">Commons PS</a>
            <a href="#Commons RTB">Commons RTB</a>
            <a href="#Mainframe Team">Mainframe Team</a>
            <a href="#Testing">Testing</a>

          </div>
         </div>

现在,我想根据下拉选择显示不同的表。如果我选择了车辆PS,则车辆PS表必​​须显示我是否从下拉列表中选择了其他选项。

有人可以帮我吗

尝试以下代码,但不起作用。

 var dropdowns = document.getElementsByClassName("dropdown-content");
          var i;

          for (i = 0; i < dropdowns.length; i++) {
           dropdowns[i].addEventListener("click", function(){
             if(dropdowns[i]=="Vehicle PS")
             {

              document.getElementById("Vehicle-PS-Software").classList.toggle('show');
             }
             else{
               document.getElementById("Vehicle-PS-Software").classList.remove('show');
             }
            });
          }

5 个答案:

答案 0 :(得分:0)

下拉菜单的正确选择器是

var dropdowns = document.querySelector('.dropdown-content').children

您可以调试其余代码并检查onClick逻辑。

答案 1 :(得分:0)

您可以尝试代码。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}
#Vehicle-PS-Software1 {display:none}
.show {display: block;}
.hide {display : none}
</style>
</head>
<body>

<h2>Clickable Dropdown</h2>
<p>Click on the button to open the dropdown menu.</p>

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <a onclick='dropDown(event)' href="#home">Vehicle PS</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div>
</div>

<table id="Vehicle-PS-Software" class='hide'>  
            <tr>
              <td width=10%>
                <input type="checkbox" name="option" value="TestID">
              </td>
              <td width=20%>
                Test ID
              </td>
            </tr>
            <tr>
              <td>
                <input type="checkbox" name="option" value="ServiceNow">
              </td>
              <td>
                ServiceNow
              </td>
            </tr>

            <tr>
              <td>
                <input type="checkbox" name="option" value="GIT">
              </td>
              <td>
                Git Hub
              </td>
            </tr>
            <tr>
              <td>
                <input type="checkbox" name="option" value="Jira">
              </td>
              <td>
                Jira
              </td>
            </tr>
            <tr>
            <tr>
              <td>
                <input type="checkbox" name="option" value="Confluence">
              </td>
              <td>
                Confluence
              </td>
            </tr>
            <tr>
              <td>
                <input type="checkbox" name="option" value="BO">
              </td>
              <td>
                BO Access
              </td>

              <td>
                <input type="file" name="sampleFile" />
              </td>
            </tr>
          </table>

<script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

var dropdowns = document.getElementsByClassName("dropdown-content");
          var i;

          for (i = 0; i < dropdowns.length; i++) {
           dropdowns[i].addEventListener("click", function(event){
document.getElementById("Vehicle-PS-Software").classList.toggle('hide');
             if(event.target.innerText=="Vehicle PS")
             {
                 document.getElementById("Vehicle-PS-Software").classList.toggle('show');
                document.getElementById("Vehicle-PS-Software").classList.toggle('hide')
             }
             else {
                    //document.getElementById("Vehicle-PS-Software").classList.remove('show');
                  //document.getElementById("Vehicle-PS-Software").classList.toggle('hide');
             }
            });
          }
</script>

</body>
</html>

答案 2 :(得分:0)

尝试将此代码添加到JS。还要从html文件中删除myFunction()。

const dropbtn = document.querySelector('.dropbtn');
dropbtn.addEventListener('click',()=>{
var myDropdown = document.querySelector('.dropdown-content');    
if (myDropdown.style.display === "block") {
 myDropdown.style.display = "none";
} else {
myDropdown.style.display = "block";
}});

答案 3 :(得分:0)

 <script>
    function myFunction() {
      var drpoptions = document.getElementById("mySelect").options;
      var e = document.getElementById("mySelect");
      var strUser = e.options[e.selectedIndex].value;


      if (strUser == "Vehicle PS") {
        document.getElementById("Vehicle-PS-Software").style.display = "block";
      }
      else {
        document.getElementById("Vehicle-PS-Software").style.display = "none";
      }


      if (strUser == "Mainframe Team") {

        document.getElementById("Mainframe-Team-Software").style.display = "block";

      }
      else {
        document.getElementById("Mainframe-Team-Software").style.display = "none";
      }

答案 4 :(得分:0)

function myFunction() {
  var e = document.getElementById("filterTodate");
  var strUser = e.options[e.selectedIndex].value;

  if (strUser == "1") {
    document.getElementById("range").style.display = "block";
    document.getElementById("tillDate").style.display = "none";
  }

  if (strUser == "2") {
    document.getElementById("tillDate").style.display = "block";
    document.getElementById("range").style.display = "none";
  }
}