基于三个下拉菜单值的 Jquery 选择器
我正在研究三个下拉菜单,每个菜单都有三个选项。这三个下拉菜单共同创建了一个可以处于二十七个状态中任何一种状态的系统。对于每个下拉列表,都有选项 1、选项 2 和全部(选项 1 和选项 2 的并集)。
根据这三个下拉菜单所处的状态,我想显示或隐藏不同的 div。这些 div 具有这种形式的类 class="distribution-(select-1)-(select-2)-(select-3)"
我正在尝试找到一种无需编写代码的 DRY 方法依赖 27 个不同的条件。
以下是下拉菜单:
<div style = "margin-top:1cm; font-size:125%">
<strong>Clickout Type </strong>
<select class = "clickouts" >
<option value = "all" selected="selected">All</option>
<option value = "clickouts" >Clickouts</option>
<option value = "preferred" >Preferred Clickouts</option>
</select>
<strong>Graph Type </strong>
<select class = "graph-type" >
<option value = "all" selected="selected">All</option>
<option value = "hist" >Histograms</option>
<option value = "percent" >Percent</option>
</select>
<strong>Roll-up Type </strong>
<select class = "roll-up" >
<option value = "all" selected="selected">All</option>
<option value = "aggregate" >Aggregate</option>
<option value = "granular" >Granular</option>
</select>
</div>
以下是 div 类的一些示例: distribution-hist-clickouts-aggregate, distribution-percent-clickouts-aggregate, class="distribution-hist-clickouts-grinar", distribution-percent-clickouts-grinar
这是前七个的 jquery状态:
$('.clickouts, .graph-type, .roll-up').change(function(){
var graph_type = $('.graph-type').val();
var roll_up = $('.roll-up').val();
var clickouts = $('.clickouts').val();
if (graph_type == 'all' & roll_up == 'all' & clickouts == 'all'){
$('div[class*="distribution"]').css('display', 'block');
}
if (graph_type == 'all' & roll_up == 'all' & clickouts == 'clickouts'){
$('div[class*="clickout"]').css('display', 'block');
$('div[class*="preferred"]').css('display', 'none');
}
else if (graph_type == 'all' & roll_up == 'all' & clickouts == 'preferred'){
$('div[class*="clickout"]').css('display', 'none');
$('div[class*="preferred"]').css('display', 'block');
}
else if (graph_type == 'hist' & roll_up == 'all' & clickouts == 'all'){
$('div[class*="hist"]').css('display', 'block');
$('div[class*="percent"]').css('display', 'none');
}
else if (graph_type == 'percent' & roll_up == 'all' & clickouts == 'all'){
$('div[class*="hist"]').css('display', 'none');
$('div[class*="percent"]').css('display', 'block');
}
else if (graph_type == 'all' & roll_up == 'aggregate' & clickouts == 'all'){
$('div[class*="aggregate"]').css('display', 'block');
$('div[class*="granular"]').css('display', 'none');
}
else if (graph_type == 'all' & roll_up == 'granular' & clickouts == 'all'){
$('div[class*="aggregate"]').css('display', 'none');
$('div[class*="granular"]').css('display', 'block');
}
});
I am working on with three drop-down menus each of which have three options. Together these three drop-down menus create a system that can be in any one of twenty seven states. For each drop-down, there is option 1, option 2, and all (which is the union of option 1 and 2).
Depending on what state these three drop-down menus are in, I want to display or hide different divs. These divs have classes of this form class="distribution-(select-1)-(select-2)-(select-3)"
I am trying to find a DRY way of coding this up without relying on 27 different conditionals.
Here are the drop-down menus:
<div style = "margin-top:1cm; font-size:125%">
<strong>Clickout Type </strong>
<select class = "clickouts" >
<option value = "all" selected="selected">All</option>
<option value = "clickouts" >Clickouts</option>
<option value = "preferred" >Preferred Clickouts</option>
</select>
<strong>Graph Type </strong>
<select class = "graph-type" >
<option value = "all" selected="selected">All</option>
<option value = "hist" >Histograms</option>
<option value = "percent" >Percent</option>
</select>
<strong>Roll-up Type </strong>
<select class = "roll-up" >
<option value = "all" selected="selected">All</option>
<option value = "aggregate" >Aggregate</option>
<option value = "granular" >Granular</option>
</select>
</div>
Here are some examples of the div classes:distribution-hist-clickouts-aggregate, distribution-percent-clickouts-aggregate, class="distribution-hist-clickouts-granular", distribution-percent-clickouts-granular
Here is the jquery for the first seven states:
$('.clickouts, .graph-type, .roll-up').change(function(){
var graph_type = $('.graph-type').val();
var roll_up = $('.roll-up').val();
var clickouts = $('.clickouts').val();
if (graph_type == 'all' & roll_up == 'all' & clickouts == 'all'){
$('div[class*="distribution"]').css('display', 'block');
}
if (graph_type == 'all' & roll_up == 'all' & clickouts == 'clickouts'){
$('div[class*="clickout"]').css('display', 'block');
$('div[class*="preferred"]').css('display', 'none');
}
else if (graph_type == 'all' & roll_up == 'all' & clickouts == 'preferred'){
$('div[class*="clickout"]').css('display', 'none');
$('div[class*="preferred"]').css('display', 'block');
}
else if (graph_type == 'hist' & roll_up == 'all' & clickouts == 'all'){
$('div[class*="hist"]').css('display', 'block');
$('div[class*="percent"]').css('display', 'none');
}
else if (graph_type == 'percent' & roll_up == 'all' & clickouts == 'all'){
$('div[class*="hist"]').css('display', 'none');
$('div[class*="percent"]').css('display', 'block');
}
else if (graph_type == 'all' & roll_up == 'aggregate' & clickouts == 'all'){
$('div[class*="aggregate"]').css('display', 'block');
$('div[class*="granular"]').css('display', 'none');
}
else if (graph_type == 'all' & roll_up == 'granular' & clickouts == 'all'){
$('div[class*="aggregate"]').css('display', 'none');
$('div[class*="granular"]').css('display', 'block');
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要将复杂的类
class="distribution-(select-1)-(select-2)-(select-3)
更改为一组类class="distribution (select- 1) (select-2) (select-3)
您可以使用以下脚本:代码:http://jsfiddle.net/9R5zj/9/
If you will change your complex class
class="distribution-(select-1)-(select-2)-(select-3)
to set of classesclass="distribution (select-1) (select-2) (select-3)
you can use the following script:Code: http://jsfiddle.net/9R5zj/9/
好的,有几件事将有助于重构这个。
您应该记住的一件简单的事情是元素可以有多个类。这意味着,您可以为 div 提供四个类,而不是使用
distribution-hist-clickouts-aggregate
类,如下所示:另外,也不要使用
value
> 将选择选项设置为all
,通过将选择器放入值中,可以使程序更容易理解:我不确定我是否完全理解所有
if 背后的逻辑
语句,但你可以对此做一些事情效果:您可以在此演示中看到我的建议。
Ok, there are several things that will help to refactor this.
An easy thing that you should remember is that elements can have more than one class. That means, instead of having a div with the
distribution-hist-clickouts-aggregate
class, you can give the div four classes, like this:Also, instead of having the
value
of the select options asall
, you make it easier for your program to understand by putting the selectors in the values:I'm not sure I completely understand the logic behind all of the
if
statements, but you can do something to this effect:You can see what I'm suggesting in action on this demo.