简体   繁体   English

基于复选框隐藏下拉选项

[英]Hide dropdown option based on checkbox

I'm in a wordpress environment, I have created some filtered post views using the plugin facet WP and I have placed two checkboxes and a dropdown filter.我在 wordpress 环境中,我使用插件 facet WP 创建了一些过滤后的帖子视图,并放置了两个复选框和一个下拉过滤器。

So I have these 2 checkboxes:所以我有这两个复选框:

<div class="facetwp-facet facetwp-facet-isola facetwp-type-checkboxes" 
data-name="isola" data-type="checkboxes">
    <div class="facetwp-checkbox" data-value="cefalonia">Cefalonia <span 
class="facetwp-counter">(11)</span>
    </div>
    <div class="facetwp-checkbox" data-value="corfu">Corfù <span     
class="facetwp-counter">(28)</span>
    </div>
</div>

And then I have this dropdown already populated by the plugin:然后我的插件已经填充了这个下拉列表:

<div class="facetwp-facet facetwp-facet-localita_di_corfu facetwp-type- 
dropdown" data-name="localita_di_corfu" data-type="dropdown">
<select class="facetwp-dropdown">
    <option value="ipsos">Ipsos</option>
    <option value="acharavi">Acharavi</option>
    <option value="dassia">Dassia</option> 
    <option value="gouvia">Gouvia</option> 
</select>
</div>

What I want is:我想要的是:

  • if I select the first checkbox Cefalonia, then show only options "ipsos" and "acharavi" in the dropdown.如果我选择第一个复选框 Cefalonia,则在下拉列表中仅显示选项“ipsos”和“acharavi”。
  • if I select the second checkbox Corfù then show only options "Dassia" and "Gouvia" in the dropdown.如果我选择第二个复选框 Corfù,则在下拉列表中仅显示选项“Dassia”和“Gouvia”。
  • if both are selected show all the related options.如果两者都被选中,则显示所有相关选项。

just need a starting point.. I have found how to do this with 2 dropdowns but not with checkboxes.. I'm not so good with javascript many thanks只需要一个起点.. 我已经找到了如何用 2 个下拉菜单而不是复选框来做到这一点.. 我不太擅长 javascript 非常感谢

Understanding: If I am not misunderstanding your scenario, you are asking for some front end development (HTML, CSS, JavaScript) to be carried out so that drop down options alter based on the users checkbox selection理解:如果我没有误解你的场景,你是在要求进行一些前端开发(HTML、CSS、JavaScript),以便下拉选项根据用户的复选框选择而改变

Modifications Explained From what I can see in your HTML , your using a set of CSS classes intended to style a DIV element as a checkbox and then set values using the data-attribute as a way of reading its value.修改解释从我在您的HTML 中看到的内容来看,您使用了一组 CSS 类,旨在将 DIV 元素设置为复选框的样式,然后使用 data-attribute 设置值作为读取其值的一种方式。 Personally I feel it is a bit long-winded and you would of been better off making them checkboxes and just style the checkbox using CSS to how ever you feel works best, if your checkboxes are sliders switches then you can include a checkbox that is hidden and that the value of the checkbox is updated from the interactions the user is making.就我个人而言,我觉得这有点冗长,你最好让它们成为复选框,并使用 CSS 设置复选框的样式,以达到你感觉最好的效果,如果你的复选框是滑块开关,那么你可以包含一个隐藏的复选框并且复选框的值是从用户进行的交互中更新的。

I have therefore made my code shier basic for you to follow, I will admit this is not the most efficient way of coding it, but its a working example for you to modify to your hearts content.因此,我让我的代码更加基础,供您遵循,我承认这不是最有效的编码方式,但它是您可以根据自己的内心内容进行修改的工作示例。

Note this does not HIDE the options but disables the user from selecting them If you want to hide the option, you will need to next each option in a DIV and set that the CSS display attribute to false.请注意,这不会隐藏选项,但会禁止用户选择它们。如果您想隐藏该选项,您需要在 DIV 中找到每个选项,并将 CSS 显示属性设置为 false。 I didnt code it this way as I would have to research how that would fit in with it being HTML compliant.我没有用这种方式编码,因为我必须研究它如何适应 HTML 兼容。

Code:代码:

 function checkState(x){ var Checkboxes = document.getElementsByName("checkbox"); var Options = document.getElementsByName("options"); for (i = 0; i < Options.length; i++){ Options[i].disabled = true; } if (Checkboxes[0].checked && Checkboxes[1].checked){ for (i = 0; i < Options.length; i++){ Options[i].disabled = false; } } if (Checkboxes[0].checked && (Checkboxes[1].checked == false)){ Options[0].disabled = false; Options[1].disabled = false; } if (Checkboxes[1].checked && (Checkboxes[0].checked == false)){ Options[2].disabled = false; Options[3].disabled = false; } }
 <form> Cefalonia: <input name="checkbox" type="checkbox" class="facetwp-checkbox" value="cefalonia" onclick="checkState(this)"><br> Corfù: <input name="checkbox" type="checkbox" class="facetwp-checkbox" value="corfù" onclick="checkState(this)"> </form> <div class="facetwp-facet facetwp-facet-localita_di_corfu facetwp-type- dropdown" data-name="localita_di_corfu" data-type="dropdown"> <select class="facetwp-dropdown"> <option name="options" value="ipsos" disabled>Ipsos</option> <option name="options" value="acharavi" disabled>Acharavi</option> <option name="options" value="dassia" disabled>Dassia</option> <option name="options" value="gouvia" disabled>Gouvia</option> </select> </div>

Understanding what is happening First off we have no ID or Names allocated to the options, this is important because you have no way of selecting the element in JavaScript without it (unless using a JS library with a function pre-coded such as jQuery, that checks every option on the page, which can slow down website performance, specially if you have multiple forms elsewhere on your website).了解正在发生的事情首先,我们没有为选项分配 ID 或名称,这很重要,因为如果没有它,您将无法在 JavaScript 中选择元素(除非使用带有预编码函数的 JS 库,例如 jQuery,检查页面上的每个选项,这会降低网站性能,特别是如果您网站上的其他地方有多个表单)。

Given that IDs are to be unique, I have given the elements a name, this way we can select all of them and compile it into an array, which we can then loop thru and disable all or enable all based on both checkboxes being selected or not.鉴于 ID 是唯一的,我给元素起了一个名字,这样我们就可以选择所有元素并将其编译成一个数组,然后我们可以循环遍历并禁用所有或启用所有基于被选中的复选框或不是。

As I am assuming these are the only two options available, I have done various if statements, otherwise it would need a switch statement which would be more efficient if there are more than just kefelonia and corfu as the options to choose from, this is because you didnt specify this in your description to your question.因为我假设这些是唯一可用的两个选项,所以我做了各种 if 语句,否则它需要一个 switch 语句,如果不仅仅是 kefelonia 和 corfu 作为可供选择的选项,它会更有效,这是因为您没有在您的问题描述中指定这一点。

We then loop thru each value and enable or disable the option based on the checkbox chosen.然后我们遍历每个值并根据选择的复选框启用或禁用该选项。

Disable them all first, that way if no boxes are chosen there all disabled If we select all options we enable all If we dont, then only display what is relevant首先禁用它们,这样如果没有选择任何框,则全部禁用 如果我们选择所有选项,我们将启用所有 如果我们不这样做,则只显示相关的内容

You can try setting the disabled attribute on the option depending on the checked value of the drop down:您可以尝试根据下拉列表的选中值在选项上设置禁用属性:

 $(".facetwp-dropdown option").each(function(){ $(this).attr('disabled','disabled'); }); $(".facetwp-dropdown").val(""); $('[data-value="cefalonia"]').change(function(){ $(".facetwp-dropdown").val(""); if($(this)[0].checked){ $(".facetwp-dropdown option[value='ipsos'],.facetwp-dropdown option[value='acharavi']").removeAttr('disabled'); } else{ $(".facetwp-dropdown option[value='ipsos'],.facetwp-dropdown option[value='acharavi']").attr('disabled','disabled'); } }); $('[data-value="corfu"]').change(function(){ $(".facetwp-dropdown").val(""); if($(this)[0].checked){ $(".facetwp-dropdown option[value='dassia'],.facetwp-dropdown option[value='gouvia']").removeAttr('disabled'); } else{ $(".facetwp-dropdown option[value='dassia'],.facetwp-dropdown option[value='gouvia']").attr('disabled','disabled'); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="facetwp-facet facetwp-facet-isola facetwp-type-checkboxes" data-name="isola" data-type="checkboxes"> <input type="checkbox" data-value="cefalonia">Cefalonia <input type="checkbox" data-value="corfu">Corfù </div> <div class="facetwp-facet facetwp-facet-localita_di_corfu facetwp-type- dropdown" data-name="localita_di_corfu" data-type="dropdown"> <select class="facetwp-dropdown"> <option value="ipsos">Ipsos</option> <option value="acharavi">Acharavi</option> <option value="dassia">Dassia</option> <option value="gouvia">Gouvia</option> </select> </div>

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

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