Jquery按多个复选框筛选表行

时间:2016-03-05 02:25:18

标签: javascript jquery wordpress jquery-ui filtering

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

以下是我一直在使用的代码:

$("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>

2 个答案:

答案 0 :(得分:0)

我认为你只是在调用第一个复选框[0]

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

通过插入像

这样的内容来调用所有内容
$('input[type=checkbox]').each(function () {
           if (this.checked) {
               console.log($(this).val()); 
           }
});

答案 1 :(得分:0)

您的代码运行正常,这是一个简化版本

&#13;
&#13;
$("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();
  }
});
&#13;
<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>
&#13;
&#13;
&#13;