简体   繁体   English

根据输入值显示或隐藏div

[英]show or hide divs depending on input value

I have an input[type=range], user can select a value (for example, from 0 to 1000), this is kinda price filter, also I have several divs each one with it's own pricevalue. 我有一个input [type = range],用户可以选择一个值(例如,从0到1000),这是一种价格过滤器,我还有几个div,每个div都有自己的pricevalue。 I need div to be shown or hide depending on input values. 我需要根据输入值显示或隐藏div。 How to do this with VanillaJS? VanillaJS如何做到这一点?

Is this hardcoded, or are they dynamic divs? 这是硬编码的,还是动态div? Have you tried just getting the value of the input and hiding the divs that are required? 您是否尝试过仅获取输入值并隐藏所需的div?

if(input.val < 1000){
    var divToHide = document.getElementById("priceMid");
    divToHide.style.display = 'none';           // Hide
} 

if you have multiple divs, give each one their own attribute with the value, and then you can iterate over them and only hide the ones above/below a certain value. 如果您有多个div,请为每个div赋予其自己的属性,然后再对其进行迭代,然后仅将其隐藏在特定值的上方/下方。 This answer has more in detail Show/hide 'div' using JavaScript 此答案有更多详细信息使用JavaScript显示/隐藏'div'

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

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