【问题标题】:The < div > filter in the < div > filter<div> 过滤器中的 <div> 过滤器
【发布时间】:2017-02-08 09:02:53
【问题描述】:

我有红色、蓝色、绿色和金色的三角形、正方形、圆形。我需要为颜色和形状做过滤器。例如,如果我选择红色和圆圈,我将只看到一个红色圆圈 此代码 HTML:

<table border="0">
<tr>
    <td width="20%" >
        <div id="triangleRed" class="color Red triangle"></div>
        <div id="triangleBlue" class="color Blue triangle"></div>
        <div id="triangleGreen" class="color Green triangle"></div>
        <div id="triangleGold" class="color Gold triangle"></div>
    </td>
   <td width="20%" align="center">
        <div id="squareRed" class="color Red square"></div>
        <div id="squareBlue" class="color Blue square"></div>
        <div id="squareGreen" class="color Green square"></div>
        <div id="squareGold" class="color Gold square"></div>
    </td>
    <td width="40%" align="center">
        <div id="circleRed" class="color Red circle"></div>
        <div id="circleBlue" class="color Blue circle"></div>
        <div id="circleGreen" class="color Green circle"></div>
        <div id="circleGold" class="color Gold circle"></div>
    </td>
    <td width="40%" >
        Filter:
        <br/>
        <div class="searchColor" id="filterColor">
            <div class="searchTextColor"> Color: </div>

            <input type="checkbox"  id="Red"  value="Red" />Red
            <br/>
            <input type="checkbox"  id="Blue"   value="Blue"/>Blue
            <br/>
            <input type="checkbox"  id="Green"  value="Green"/>Green
            <br/>
            <input type="checkbox" id="Gold"   value="Gold"/>Gold
            <p/>
        </div>
        <div class="searchColor" id="searchShape">
            <div class="searchShape"> Shape:</div>
            <div class="paintSelect">
                <input type="checkbox" id="triangle"  value="triangle" />triangle
                <br/>
                <input type="checkbox" id="circle"  value="circle"/>circle
                <br/>
                <input type="checkbox" id="square" value="square"/>square
            </div>
        </div>
    </td>
</tr>

我已经为过滤器编写了这段代码:

$(document).ready(function() {

$("div[class='searchColor'] input").change(function () {
        var k = this.value;
    switch ($('input:checked').length) {

        case 0:
            $('.color').show();
            return;
        case 1:
            if (this.checked) {
                $('.color').hide();
            }
    }
    if($("div [class='paintSelect'] input").checked){
                    if (this.checked) {
                        $('.' + this.value).show();
                    } else {
                        $('.' + this.value).hide();
                    }
            }


    $('.' + this.value).toggle();

});
});

我收到了这个:https://jsfiddle.net/

但代码运行不正确。如果您选择颜色之后的形状或相反。你可以看到不正确的答案。 我的错在哪里?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    你需要考虑4个场景

    1 : 未选择颜色和形状

    2 : 选择颜色但不选择形状

    3 : 选择形状但不选择颜色

    4 : 都被选中

    然后您可以显示全部并隐藏未选中或隐藏全部并显示所有选中我正在使用第一种方式

      if($("#filterColor input:checked").length == 0 && $("#searchShape input:checked").length == 0){
            $('.color').show();
        }else if($("#filterColor input:checked").length == 0 && $("#searchShape input:checked").length > 0){
                $('.color').show();  
             $("#searchShape input:not(:checked)").each(function() {
                        $('.' + $(this).attr('value')).hide();
                        });
        }else if($("#filterColor input:checked").length > 0 && $("#searchShape input:checked").length == 0){
            $('.color').show();
          $("#filterColor input:not(:checked)").each(function() {
           //console.log(this,$(this).attr('value'),$('.'+$(this).attr('value')))
                        $('.' + $(this).attr('value')).hide();
                        });
        }else{
            $('.color').show();
    
           $("#searchShape input:not(:checked)").each(function() {
                        $('.' + $(this).attr('value')).hide();
                        });
    
             $("#filterColor input:not(:checked)").each(function() {
           //console.log(this,$(this).attr('value'),$('.'+$(this).attr('value')))
                        $('.' + $(this).attr('value')).hide();
                        });
    
    
    
        }
    

    小提琴在这里https://jsfiddle.net/y2b3qebr/15/

    【讨论】:

    • 分解问题然后编写代码的好方法,而不是在无数次失败后开始编写然后分解问题。 +1
    猜你喜欢
    • 2014-04-30
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2015-05-26
    • 1970-01-01
    相关资源
    最近更新 更多