简体   繁体   English

使用 HTML/Javascript 中的下拉菜单显示列表

[英]Display list using dropdown menu in HTML/Javascript

Each option within the select tag is a different list that I currently have hidden from being displayed on the page. select 标签中的每个选项都是一个不同的列表,我目前已将其隐藏在页面上。 I want to display the list on my page depending on what the user selects from the dropdown.我想根据用户从下拉列表中选择的内容在我的页面上显示列表。 The javascript currently only displays the ID of the list in the console log. javascript 目前只在控制台日志中显示列表的 ID。

 <script> document.addEventListener('DOMContentLoaded', () => { document.getElementById('myDropdown').addEventListener('input', handleSelect); }); function handleSelect(ev) { let select = ev.target; console.log(select.value); } function handleData(ev) { let theInput = ev.target; } </script>
 <label for="curlinks">Curriculum Link:</label> <select id="myDropdown"> <option value="all">All Content</option> <option value="art">Art</option> <option value="business">Business and Enterprise</option> <option value="computing">Computing</option> <option value="english">English and Literacy</option> <option value="human">Humanities</option> <option value="languages">Languages</option> <option value="maths">Maths</option> <option value="numeracy">Numeracy and Finance</option> <option value="science">Science</option> <option value="service">Service and Health</option> <option value="tech">Technology and Engineering</option> </select>

The easiest way to do this is to use jquery functions.hide() and.show() (could be also.fadeout() or fadein() if you want it animated).最简单的方法是使用 jquery functions.hide() 和 .show() (如果你想要动画,也可以是.fadeout() 或 fadein())。
But if you want to use plain javascript instead of jquery try this: https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp但如果你想使用普通的 javascript 而不是 jquery 试试这个: https://www.w3schools.com/howto/howto_js_toggle.

Jquery// First set a data- attribute and value (which are equal with select tag values) to each product rows. Jquery// 首先为每个产品行设置一个数据属性和值(与 select 标记值相等)。 The logic is when the product data-attribute value matches the select tag value, the product hides unselected products.逻辑是当产品数据属性值与 select 标签值匹配时,产品隐藏未选中的产品。

Set all your product a "productDiv" class and data-attribute and values which matches select tag values将您的所有产品设置为“productDiv”class 和与 select 标签值匹配的数据属性和值

$(document).ready(function() {
$("#myDropdown").on("change", function () {
    $(".productDiv").hide();
    $(".productDiv[data-attribute=' "+ $(this).val()+" ' ").show();
}) ;
}) ;

Try something like this:尝试这样的事情:

 document.addEventListener('DOMContentLoaded', () => { document.getElementById('myDropdown').addEventListener('input', handleSelect); }); function handleSelect(ev) { let select = ev.target; console.log(select.value); let list = document.getElementById(select.value); list.style.display = 'block'; } function handleData(ev) { let theInput = ev.target; }
 <label for="curlinks">Curriculum Link:</label> <select id="myDropdown"> <option value="all">All Content</option> <option value="art">Art</option> <option value="business">Business and Enterprise</option> <option value="computing">Computing</option> <option value="english">English and Literacy</option> <option value="human">Humanities</option> <option value="languages">Languages</option> <option value="maths">Maths</option> <option value="numeracy">Numeracy and Finance</option> <option value="science">Science</option> <option value="service">Service and Health</option> <option value="tech">Technology and Engineering</option> </select> <:-- The lists --> <div id="art" style="display: none">Content of art's list</div> <div id="business" style="display. none">Content of business's list</div> <.-- etc... -->

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

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