2016-03-05 72 views
0

我正在尝试获取表格的过滤器。我想要的是复选框根据它们是否被选中来筛选(隐藏)行,以及该值是否存在于表格单元格中。我有点工作,但它只会对其中一个复选框进行排序。另外,如果表格单元格中有多个值被过滤,则该行将被隐藏。思考?预先感谢您的任何帮助。出于某种原因,我真的很困难!Jquery通过多个复选框过滤表格行

这是我一直在使用的代码:

$("input:checkbox").click(function() { 
var showAll = true; 
$('tr').not('.first').hide(); 
$('input[type=checkbox]').each(function() { 
    if ($(this)[0].checked) { 
     showAll = false; 
     var status = $(this).attr('rel'); 
     var value = $(this).val();    
     $('td.' + 'status' + '[rel="' + value + '"]').parent('tr').show(); 

    } 
}); 
    if(showAll){ 
    $('tr').show(); 
} 
}); 

的标记如下(请原谅一些混乱,其WordPress的)

<div class="grants-filter"> 
<h3 class="filter-title">Filter Scholarships</h3><br/> 
<h3>Year of Study:</h3> 
<input type="checkbox" name="chx" rel="status" value="Freshman">Freshman 
<input type="checkbox" name="chx" rel="status" value="Sophmore">Sophmore 
<input type="checkbox" name="chx" rel="status" value="Junior">Junior 
<input type="checkbox" name="chx" rel="status" value="Senior">Senior 
<br/><br/><h3>Duration:</h3> 
<input type="checkbox" rel="duration" value="Summer">Summer 
<input type="checkbox" rel="duration" value="Semester">Semester 
<input type="checkbox" rel="duration" value="Full Year or More">Full Year or More 


</div> 

<div class="grants-listing"> 


<table border="1" id="table" class="grants-table"> 
<thead> 
<tr class="first"> 
<th>Title</th> 
<th>Description</th> 
<th>Field</th> 
<th>Duration</th> 
<th>Year of Study</th> 
<th>Other</th> 
<th>Procedure</th> 
<th>URL</th> 
</tr> 
</thead> 
<tbody> 
<?php 
$grargs = array ('post_type' => 'scholarship'); 
$grant_query = new WP_Query($grargs); 
while ($grant_query -> have_posts()): 
$grant_query -> the_post(); ?> 
<tr class="grant-detail"> 

<td> 
<?php the_title(); ?> 
</td> 
<td><?php echo wp_trim_words(get_the_content(), 25); ?></td> 
<td class="fields" rel="<?php echo get_field('fields'); ?>"><?php echo the_field('fields'); ?></td> 
<td class="duration" rel="<?php echo the_field('desired_duration'); ?>"><?php echo the_field('desired_duration'); ?></td> 
<td class="status" rel="<?php echo the_field('year-of-study'); ?>"><?php echo the_field('year-of-study'); ?></td> 
<td class="other" rel="<?php echo the_field('other'); ?>"><?php echo the_field('other'); ?></td> 
<td class="procedure" rel="<?php echo the_field('procedure'); ?>"><?php echo the_field('procedure'); ?></td> 
<td class="url"><a href="<?php echo get_field('url'); ?>"><?php echo get_field('url'); ?></a></td> 
</tr> 
<?php endwhile; 
wp_reset_postdata(); //reset the first query 
?> 

</tbody> 
</table> 

回答

0

正如我认为你只是在呼唤第一复选框[0]

if ($(this)[0].checked) { 

呼叫所有由相同的

插入成才
$('input[type=checkbox]').each(function() { 
      if (this.checked) { 
       console.log($(this).val()); 
      } 
}); 
0

你的代码工作正常,这里是一个简化版本

$("input:checkbox").click(function() { 
 
    var showAll = true; 
 
    $('tr').not('.first').hide(); 
 
    $('input:checkbox:checked').each(function() { 
 
    showAll = false; 
 
    var status = $(this).attr('rel'); 
 
    var value = $(this).val(); 
 
    $('td.' + 'status' + '[rel="' + value + '"]').parent('tr').show(); 
 
    }); 
 
    if (showAll) { 
 
    $('tr').show(); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="grants-filter"> 
 
    <h3 class="filter-title">Filter Scholarships</h3> 
 
    <br/> 
 
    <h3>Year of Study:</h3> 
 
    <input type="checkbox" name="chx" rel="status" value="Freshman">Freshman 
 
    <input type="checkbox" name="chx" rel="status" value="Sophmore">Sophmore 
 
    <input type="checkbox" name="chx" rel="status" value="Junior">Junior 
 
    <input type="checkbox" name="chx" rel="status" value="Senior">Senior 
 
</div> 
 

 
<div class="grants-listing"> 
 

 

 
    <table border="1" id="table" class="grants-table"> 
 
    <thead> 
 
     <tr class="first"> 
 
     <th>Title</th> 
 
     <th>Description</th> 
 
     <th>Fields</th> 
 
     <th>Status</th> 
 
     <th>Procedure</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr class="grant-detail"> 
 
     <td> 
 
      Name 
 
     </td> 
 
     <td>Description</td> 
 
     <td class="fields" rel="Freshman">Freshman</td> 
 
     <td class="status" rel="Freshman">Freshman</td> 
 
     <td class="procedure" rel="Freshman">Freshman</td> 
 
     </tr> 
 
     <tr class="grant-detail"> 
 
     <td> 
 
      Name 
 
     </td> 
 
     <td>Description</td> 
 
     <td class="fields" rel="Sophmore">Sopho</td> 
 
     <td class="status" rel="Sophmore">Sopho</td> 
 
     <td class="procedure" rel="Sophmore">Sopho</td> 
 
     </tr> 
 
     <tr class="grant-detail"> 
 
     <td> 
 
      Name 
 
     </td> 
 
     <td>Description</td> 
 
     <td class="fields" rel="Junior">Junior</td> 
 
     <td class="status" rel="Junior">Junior</td> 
 
     <td class="procedure" rel="Junior">Junior</td> 
 
     </tr> 
 
    </tbody> 
 
    </table>

+0

谢谢你的快速响应!我遇到的问题是,表中的某些td将具有多个值(例如,新生,新生和初中)。例如,选择Sophmore将不会显示具有多个值的行,即使sophmore是其中一个值。思考? – cthomas1978

+0

有关如何让过滤器识别值是否存在于td中的多个值内的任何想法? – cthomas1978

+0

对不起,我无法得到你的观点,你能提供一个示例输出html(而不是php),td中的多个值是什么意思?会不会像'大二,大二'? –