简体   繁体   English

根据多个下拉选择显示 Div

[英]Show Div based on multiple drop down selections

<select name="main">
  <option value="1">1</option>
  <option value="1">2</option>
  <option value="1">3</option>
  <option value="1">4</option>
  <option value="1">5</option>
</select>
<select name="sub">
  <option value="1">1</option>
  <option value="1">2</option>
  <option value="1">3</option>
</select>

I have 2 seperate drop downs as seen here.如此处所示,我有 2 个单独的下拉菜单。 I want to make it so that if they select main it will show a div for main with a text box equal to they number they select(ie. 4 will show 4 main textboxes for them to fill out) which I know how to do.我想这样做,如果他们选择 main 它将显示一个 main 的 div 文本框等于他们选择的编号(即 4 将显示 4 个主要文本框供他们填写),我知道该怎么做。 The problem comes with sub I want them to show a text box equal to the sub for each main select(ie if main is 3 and sub is 3 then each of the 3 main text box will be followed by 3 sub text boxes) I can make it show 3 or all but I can not figure out how to make it go based of the selection of both main and sub to ive me the exact fields I am looking for.问题来自 sub 我希望他们为每个主选择显示一个等于 sub 的文本框(即,如果 main 是 3,sub 是 3,那么 3 个主文本框中的每一个都将跟随 3 个子文本框)我可以让它显示 3 个或全部,但我无法根据主要和次要的选择来弄清楚如何让它显示我正在寻找的确切字段。 Do I just need to set in the function for the java script that if main == x then show xyz on the function for sub?我是否只需要在 java 脚本的函数中设置如果 main == x 然后在 sub 的函数上显示 xyz ?

In order to accomplish this you'll have to:为了实现这一点,您必须:

  • Use the change event on each select在每个选择上使用change事件
  • Take advantage of the value attribute on each option利用每个选项的value属性

Demo:演示:

 const mainSelect = document.querySelector('select[name="main"]'); const subSelect = document.querySelector('select[name="sub"]'); const resultsDiv = document.getElementById("results"); mainSelect.addEventListener("change", event => { handleChange(event); }); subSelect.addEventListener("change", event => { handleChange(event); }); function handleChange(event) { let textboxes = ""; let mainCount = Number(mainSelect.value); let subCount = Number(subSelect.value); for (let i = 0; i < mainCount; i++) { let t = "<input type='text' placeholder='main' /><br/>"; for (let s = 0; s < subCount; s++) { t += "<input placeholder='sub' style='margin-left: 10px;' /><br/>" } textboxes += t; } resultsDiv.innerHTML = `<h4>Please fill these out</h4>${textboxes}`; }
 <select name="main"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <br /> <select name="sub"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <div id="results"></div>

You can use onchange in the sub select:您可以在子选择中使用 onchange:

<select name="sub" onchange="createSubInput(this)">

 function createSubInput (selected) {
      let mainSelectedValue = document.getElementById('main').value;

       for(let i=0; i< mainSelectedValue; i++) {
        for(let j=0; j< selected.value; j++) {
          // todo: create sub input by each main input
        }
      }
    }

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

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