Jquery优化表的结果:两个或多个逻辑语句

时间:2016-02-11 15:10:47

标签: javascript jquery html

我想隐藏表的值,如果它们匹配两个语句 在我的refine()函数中,这两个语句单独工作,但如果它们一起运行,则以第二次接管为止,

我想要一种说法

如果语句和语句b为真,则同时执行

<html>
    <body>

    <select id="gender" name="gender"> 
    <option value="">Both</option>
    <option value="M">Male</option>
    <option value="F">Female</option>
    </select>

    <select id="region" name="region"> 
    <option value="">All Regions</option>
    <option value="East Midlands">East Midlands</option>
    <option value="Greater London">Greater London</option>
    <option value="Northern Ireland">Northern Ireland</option>
    <option value="North West">North West</option>
    <option value="North Midlands">North Midlands</option>
    <option value="Scotland">Scotland</option>
    <option value="South East">South East</option>
    <option value="South Midland">South Midlands</option>
    <option value="South West">South West</option>
    <option value="West Midlands">West Midlands</option>
    <option value="Yorkshire & North East">Yorkshire & North East</option>
    </select>

    <button id="button" onClick="refine()">Refine Results</button>
    </body>
    </html>

    <script>

    function refine(){

       var gender = document.getElementById('gender').value;
       var region = document.getElementById('region').value;

       $('#t01 tbody tr').each(function() {
        $(this).toggle($('td:eq(1):contains(' + gender + ')', this).length > 0);
      })

       $('#t01 tbody tr').each(function() {
        $(this).toggle($('td:not(:contains(' + region + ')', this).length > 0);
      })
    }
    </script>

编辑后:

想象一下这就是桌子

M大伦敦

F大伦敦

M西北部

F西北

如果用户选择男性+西北

它只会显示

西北地区

1 个答案:

答案 0 :(得分:0)

如果我理解正确的话。这可能会对你有所帮助

    function refine(){

        var gender = document.getElementById('gender').value;
        var region = document.getElementById('region').value;

        $('#t01 tbody tr').each(function() {
            var firstMatch = $('td:eq(1):contains(' + gender + ')', this).length > 0;
            var secondMatch = $('td:not(:contains(' + region + ')', this).length > 0;

            if (firstMatch && secondMatch) {
                $(this).toggle(firstMatch);
                $(this).toggle(secondMatch);
            }
        })
    }

如果没有,请更具体一点。