简体   繁体   English

如何根据前一个下拉列表和最终选择显示文本框创建下拉列表

[英]How do I create a drop down list dependant on previous drop down and final choice displays text box

How do I create a drop down list dependant on previous drop down and final choice displays text box? 如何根据前一个下拉列表和最终选择显示文本框创建一个下拉列表?

EG 1st drop down has a choice of 1, 2 & 3. 2nd list had a choice of A,B,C if I chose 1 from previous, D, E, F if I chose 2, or X,Y, Z if I chose 3. Then if I chose any letter a text box displays with a message, (a different message for each letter choice)? EG 1st下拉菜单可以选择1、2和3。如果我从上一个列表中选择1,则第二个列表可以选择A,B,C;如果我选择2,则选择D,E,F;如果我选择X,Y,Z选择3。然后,如果我选择了任何字母,则会显示一个带有消息的文本框(每个字母选择都显示不同的消息)?

Hope this makes sense. 希望这是有道理的。 Thanks. 谢谢。

Several different ways come to mind: (I will explain using Bootstrap examples, other frameworks can be used or you can write a custom HTML/CSS/JS for yourself) 我想到了几种不同的方式:(我将使用Bootstrap示例进行说明,可以使用其他框架,或者您可以自己编写自定义HTML / CSS / JS)

Multiple Navigations: You select a tab in the 1st row then another row of selection appears below depending on what you selected in the top row. 多个导航:您在第一行中选择一个标签,然后根据您在第一行中的选择在下方显示另一行选择。 Repeat once more for the 3rd row. 对第三行再次重复一次。

see Bootstrap Navigation tabs/pills 参见引导导航标签/药丸

Multiple dropdowns side by side left to right: You select 1st then when the user picks the value second one appears containing only options for that value and same goes for 3rd.. 从左到右并排显示多个下拉列表:选择第一个,然后在用户选择值时,第二个出现,仅包含该值的选项,第三个出现。

Bootstrap dropdowns with or without nesting 具有或不具有嵌套的Bootstrap下拉列表

Multiple accordions: You have 3 levels of accordion nested in each other. 多个手风琴:您有3个级别的手风琴相互嵌套。 As user picks one it expands revealing further options only available for that previous choice. 当用户选择一个时,它会展开并显示仅适用于该先前选择的其他选项。

Bootstrap collapsable and accordion Bootstrap可折叠和手风琴

Dropdown with an accordion with submenus: Elegant mix of 2 beforementioned, dropdown contains 2 levels of an accordion for every item. 带有子菜单的手风琴的下拉菜单:前面提到的2的优雅组合,下拉菜单每个项目都包含2个级别的手风琴。 you need to redo the dropdown that it only closes when an item in 2nd level in accordion is selected 您需要重做下拉菜单,该下拉菜单仅在选择第二手风琴中的项目时关闭

Nested dropdowns to left: nested dropdowns example 左侧的 嵌套下拉列表 嵌套下拉列表示例

As for structuring the data I would go for nested objects (JSON like), you nest the 3rd selection into categories made of 2nd selection then altogather in 1st selection. 至于为嵌套对象(类似于JSON)提供的数据结构,您可以将第三个选择嵌套到由第二个选择组成的类别中,然后将其汇总到第一个选择中。

This is one of the ways in pure javascript. 这是纯JavaScript的方法之一。 Look into this sample implementation for some guidance to proceed further. 查看此示例实现以获取一些指导,以进一步进行操作。

1) Create necessary html markup with two select and one input. 1)用两个选择和一个输入创建必要的html标记。
2) Handle change event on first select. 2)在第一次选择时处理更改事件。
3) In change event handler, set the selected index same as that of first and populate input control. 3)在change事件处理程序中,将选定的索引设置为与第一个相同,并填充输入控件。

 var first = document.getElementById('first'); var second = document.getElementById('second'); var third = document.getElementById('third'); first.addEventListener('change', handleChangeEvent); function handleChangeEvent() { var index = this.selectedIndex; second.selectedIndex = index; var inputValue = second.options[index].value; if(inputValue == 'A,B,C') { third.value = 'Red'; } else if (inputValue == 'D,E,F') { third.value = 'Blue'; } else { third.value = 'Green'; } } 
 <select id="first"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <select id="second"> <option value="A,B,C">A,B,C</option> <option value="D,E,F">D,E,F</option> <option value="X,Y,Z">X,Y,Z</option> </select> <input type="text" id="third" /> 

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

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