简体   繁体   English

JavaScript多个条件不起作用

[英]javascript multiple conditions don't work

I have a dropdown list ,every option of the list have a year and a month . 我有一个下拉列表,列表中的每个选项都有一年零一个月。 I want to sort a select options according to year or month or both that are . 我想根据年份或月份或两者都对选择选项进行排序。 so I can display options sorted by year or month or year and month together or show everything . 所以我可以显示按年份或月份或年份和月份排序的选项,或显示所有内容。 I have the following event : 我有以下事件:

     $('#years,#months').bind('change', function () {
         y = $('#years').val();
         m = $('#months').val();
         $('#files > option').each(function () {
             $(this).show();
         });
         $('#files > option').each(function () {
             string = $(this).text();
              //first situation 
             if (y == 'All' && m == 'All')
                $(this).show();
            // second situation 
             else if ((y == 'All') && string.indexOf(m) == -1) 
                    $(this).hide();
            //third situation 
             else if ( string.indexOf(y) == -1 && m == 'All') 
                  $(this).hide();
             // fourth situation 
             else if (string.indexOf(y) == -1 || string.indexOf(m) == -1) 
               $(this).hide();
         });
     });

with html code : 带有html代码:

<select id='years'>
    <option value='All'>All</option>
    <option value='2013'>2013</option>
    <option value='2012'>2012</option>
</select>
<select id='months'>
    <option value='All'>All</option>
    <option value='Feb'>Feb</option>
    <option value='Jan'>Jan</option>
</select>
<br/>
<br/>
<select id='files' siz='10'>
    <option value='0'>sdgsdfsadfsd1-2013-Feb-1212</option>
    <option value='1'>fsadfadsfadsfadsf2-2012-Feb-123123</option>
    <option value='2'>fsadfadsfadsfadsf3-2012-Jan-123123</option>
    <option value='3'>fsadfadsfadsfadsf4-2013-Jan-123123</option>
    <option value='4'>fsadfadsfadsfadsf5-2012-Feb-123123</option>
    <option value='5'>fsadfadsfadsfadsf6-2013-Feb-123123</option>
    <option value='6'>fsadfadsfadsfadsf7-2013-Feb-123123</option>
    <option value='7'>fsadfadsfadsfadsf8-2013-Jan-123123</option>
    <option value='8'>fsadfadsfadsfadsf9-2012-Jan-123123</option>
    <option value='9'>fsadfadsfadsfadsf10-2013-Feb-123123</option>
    <option value='10'>fsadfadsfadsfadsf11-2012-Feb-123123</option>
</select>

second and third situations are not working !! 第二和第三种情况不起作用! it deals with the two conditions (true && false) , (false && true ) as they are same values ! 它处理两个条件(true && false),(false && true),因为它们是相同的值! I have no idea why ! 我不知道为什么 ! here's a Demo 这是一个演示

Your logic is wrong; 你的逻辑是错误的; the last condition is being evaluated when it shouldn't be. 最后一个条件在不应该被评估时进行评估。 (For instance: if y == 'All' and also string.indexOf(m) >= 0 ). (例如:如果y == 'All'以及string.indexOf(m) >= 0 )。 Try this instead: 尝试以下方法:

$('#files > option').each(function () {
    var string = $(this).text();
    var show;
    if (y == 'All') {
        show = m == 'All' || string.indexOf(m) >= 0;
    } else if (m == 'All') {
        show = string.indexOf(y) >= 0;
    } else {
        show = string.indexOf(y) >= 0 && string.indexOf(m) >= 0;
    }
    if (show) $(this).show();
    else $(this).hide();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM