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

时间:2011-09-22 18:32:25

标签: javascript jquery html jquery-selectors css-selectors

我正在处理三个下拉菜单,每个菜单都有三个选项。这三个下拉菜单共同创建了一个系统,可以处于二十七个状态中的任何一个状态。对于每个下拉列表,有选项1,选项2和all(选项1和2的并集)。

根据这三个下拉菜单的状态,我想显示或隐藏不同的div。这些div具有此格式class="distribution-(select-1)-(select-2)-(select-3)"

的类

我试图找到一种干扰编码方式,而不依赖于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-granular", distribution-percent-clickouts-granular

这是前七个州的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');
    }
});

2 个答案:

答案 0 :(得分:2)

如果要将复杂类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/

答案 1 :(得分:1)

好的,有几件事可以帮助重构这个。

你应该记住的一件容易的事情就是元素可以有多个类。这意味着,您可以给div四个类,而不是使用distribution-hist-clickouts-aggregate类的div,而不是:

<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();

您可以在this demo上看到我的建议。