基于三个下拉菜单值的 Jquery 选择器

发布于 2024-12-06 07:05:07 字数 3235 浏览 0 评论 0原文

我正在研究三个下拉菜单,每个菜单都有三个选项。这三个下拉菜单共同创建了一个可以处于二十七个状态中任何一种状态的系统。对于每个下拉列表,都有选项 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&nbsp;</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&nbsp;</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&nbsp;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼波传意 2024-12-13 07:05:07

如果您要将复杂的类 class="distribution-(select-1)-(select-2)-(select-3) 更改为一组类 class="distribution (select- 1) (select-2) (select-3) 您可以使用以下脚本:

$('.clickouts, .graph-type, .roll-up').change(function(){
    var graph_type = $('.graph-type').val(); 
    var roll_up = $('.roll-up').val();
    var clickouts = $('.clickouts').val(); 

    graph_type = (graph_type == "all" ? "" : "." + graph_type);
    roll_up = (roll_up == "all" ? "" : "." + roll_up);
    clickouts = (clickouts == "all" ? "" : "." + clickouts);

    var selector = '#container div' + graph_type + roll_up + clickouts;

    $('#container div:visible').hide();
    $(selector).show();
});

代码:http://jsfiddle.net/9R5zj/9/

If you will change your complex class class="distribution-(select-1)-(select-2)-(select-3) to set of classes class="distribution (select-1) (select-2) (select-3) you can use the following script:

$('.clickouts, .graph-type, .roll-up').change(function(){
    var graph_type = $('.graph-type').val(); 
    var roll_up = $('.roll-up').val();
    var clickouts = $('.clickouts').val(); 

    graph_type = (graph_type == "all" ? "" : "." + graph_type);
    roll_up = (roll_up == "all" ? "" : "." + roll_up);
    clickouts = (clickouts == "all" ? "" : "." + clickouts);

    var selector = '#container div' + graph_type + roll_up + clickouts;

    $('#container div:visible').hide();
    $(selector).show();
});

Code: http://jsfiddle.net/9R5zj/9/

只怪假的太真实 2024-12-13 07:05:07

好的,有几件事将有助于重构这个。

您应该记住的一件简单的事情是元素可以有多个类。这意味着,您可以为 div 提供四个类,而不是使用 distribution-hist-clickouts-aggregate 类,如下所示:

<div class="distribution hist clickouts aggregate"></div>

另外,也不要使用 value > 将选择选项设置为 all,通过将选择器放入值中,可以使程序更容易理解:

<option value = ".aggregate, .granular" selected="selected">All</option>    
<option value = ".aggregate" >Aggregate</option>    
<option value = ".granular" >Granular</option>    

我不确定我是否完全理解所有 if 背后的逻辑 语句,但你可以对此做一些事情效果:

$("div.distribution").hide();
$(graph_type).show();
$(roll_up).show();
$(clickouts).show();

您可以在此演示中看到我的建议。

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:

<div class="distribution hist clickouts aggregate"></div>

Also, instead of having the value of the select options as all, you make it easier for your program to understand by putting the selectors in the values:

<option value = ".aggregate, .granular" selected="selected">All</option>    
<option value = ".aggregate" >Aggregate</option>    
<option value = ".granular" >Granular</option>    

I'm not sure I completely understand the logic behind all of the if statements, but you can do something to this effect:

$("div.distribution").hide();
$(graph_type).show();
$(roll_up).show();
$(clickouts).show();

You can see what I'm suggesting in action on this demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文