带有多个过滤器的jQuery Checkbox

时间:2017-12-15 16:09:08

标签: javascript jquery html checkbox

我正在尝试使用check boxes添加两个过滤选项。第一个过滤divs根据事物分配给谁(在这种情况下为NangleFahon)和第二个基于状态的过滤器(在这种情况下为{{1} }或Todo)。以下是我的代码。



Pending Us

$(document).ready(function(e) {
  $('input[type="checkbox"]').click(function() {
    var inputValue = $(this).attr("value");
    $("." + inputValue).toggle();
  });
});




2 个答案:

答案 0 :(得分:1)

由于您在说明中使用了OR Nangle或Fahon ),这意味着您必须使用radio代替checkbox,显示/隐藏div很简单:

$('input[type="radio"]').change(function() {
    var classes = $('input[type="radio"]:checked').map(function() {
      return $(this).val();
    }).get();

    $('#stats-container>div').hide(); //Hide all
    $("." + classes.join('.')).show(); //Show filtred
});

注意:在我的示例中为您的容器添加stats-container标识符,这样您就可以在每个过滤器之前隐藏所有div。

$(document).ready(function(e) {
  $('input[type="radio"]').change(function() {
    var classes = $('input[type="radio"]:checked').map(function() {
      return $(this).val();
    }).get();

    $('#stats-container>div').hide(); //Hide all
    $("." + classes.join('.')).show(); //Show filtred
  });
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="span6">
  <div class="portlet box blue checkbox-assigned">
    <div class="portlet-title">
      <div class="caption"><i class="icon-globe"></i>Jobs</div>
    </div>

    <label><div class="checker"><span class="checked"><input type="radio" name="radio1" value="76" checked=""></span></div> Fahon</label>

    <label><div class="checker"><span class="checked"><input type="radio" name="radio1" value="60" checked=""></span></div> Nangle</label>


    <label><div class="checker"><span class="checked"><input type="radio" name="radio2" value="Todo" checked=""></span></div> Todo</label>

    <label><div class="checker"><span class="checked"><input type="radio" name="radio2" value="Pending Us" checked=""></span></div> Pending Us</label>
  </div>
  <div style="height: 500px; overflow-y: scroll" id="stats-container">
    <div class=" 76 Todo">


      <div class="dashboard-stat green jobsCard" data-assigned="Fahon" data-status="Todo">
        <div>
          Shop Name: <strong style="padding-right: 3em">Pizza</strong> Job Name: <strong>Machine not working</strong> -
          <strong>Todo</strong>
          <a style="float: right; color: #000000;">Priority: <strong>Normal</strong></a> </div>

        <div>
          Created By: <strong>Nangle</strong>
          <a style="float: right; color: #000000;">Assigned To: <strong>Fahon</strong></a>

        </div>
        <div>
          Created On <strong>24/10/2017</strong> Last Updated: <strong>24/10/2017 11:14</strong>
          <a style="float: right; color: #000000;" href="index.php?/job/view/43"><strong>View</strong></a>
        </div>
      </div>
      <div class="containerr collapsed">
        <div class="headerTab"></div>
        <div class="content dashboard-stat blue">
          <p> message </p>
        </div>
      </div>
    </div>
    <div class=" 60 Todo">


      <div class="dashboard-stat green jobsCard" data-assigned="Fahon" data-status="Todo">
        <div>
          Shop Name: <strong style="padding-right: 3em">The Castle</strong> Job Name: <strong>Accounts</strong> -
          <strong>Todo</strong>
          <a style="float: right; color: #000000;">Priority: <strong>Normal</strong></a> </div>

        <div>
          Created By: <strong>Nangle</strong>
          <a style="float: right; color: #000000;">Assigned To: <strong>Nangle</strong></a>

        </div>
        <div>
          Created On <strong>24/10/2017</strong> Last Updated: <strong>24/10/2017 11:19</strong>
          <a style="float: right; color: #000000;" href="index.php?/job/view/44"><strong>View</strong></a>
        </div>
      </div>
      <div class="containerr collapsed">
        <div class="headerTab"></div>
        <div class="content dashboard-stat blue">
          <p> message </p>
        </div>
      </div>
    </div>
    <div class=" 76 Todo">


      <div class="dashboard-stat green jobsCard" data-assigned="Fahon" data-status="Todo">
        <div>
          Shop Name: <strong style="padding-right: 3em">Time</strong> Job Name: <strong>Marketing Visit</strong> -
          <strong>Todo</strong>
          <a style="float: right; color: #000000;">Priority: <strong>Normal</strong></a> </div>

        <div>
          Created By: <strong>Nangle</strong>
          <a style="float: right; color: #000000;">Assigned To: <strong>Fahon</strong></a>

        </div>
        <div>
          Created On <strong>25/10/2017</strong> Last Updated: <strong>25/10/2017 10:32</strong>
          <a style="float: right; color: #000000;" href="index.php?/job/view/51"><strong>View</strong></a>
        </div>
      </div>
      <div class="containerr collapsed">
        <div class="headerTab"></div>
        <div class="content dashboard-stat blue">
          <p> message </p>
        </div>
      </div>
    </div>
  </div>
</div>
</div>

答案 1 :(得分:1)

&#13;
&#13;
$(document).ready(function() {
  var $checkboxes = $('input:checkbox'); //get all the checkboxes
  var $fields = $($checkboxes.map(function(){ return '.'+ this.value; }).get().join(',')); //get all the fields
  
  $checkboxes.on('click', function(e) {
    if (e.target.checked) {
      //if you recheck a filter, we need to reevaluate all the checkboxes so we don't re-check one that should not be
      $fields.show();
      $checkboxes.not(':checked').each(function(){
        $fields.filter('.'+ this.value).hide();
      });
    } else {
      //if you uncheck a filter, just hide the related fields
      $fields.filter('.'+ e.target.value).hide();
    }
  });
});
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="span6">
  <div class="portlet box blue checkbox-assigned">
    <div class="portlet-title">
      <div class="caption"><i class="icon-globe"></i>Jobs</div>
    </div>

    <label><div class="checker"><span class="checked"><input type="checkbox" name="colorCheckbox" value="76" checked=""></span></div> Fahon</label>

    <label><div class="checker"><span class="checked"><input type="checkbox" name="colorCheckbox" value="60" checked=""></span></div> Nangle</label>


    <label><div class="checker"><span class="checked"><input type="checkbox" name="colorCheckbox" value="Todo" checked=""></span></div> Todo</label>

    <label><div class="checker"><span class="checked"><input type="checkbox" name="colorCheckbox" value="Pending Us" checked=""></span></div> Pending Us</label>
  </div>
  <div style="height: 500px; overflow-y: scroll">
    <div class=" 76 Todo">


      <div class="dashboard-stat green jobsCard" data-assigned="Fahon" data-status="Todo">
        <div>
          Shop Name: <strong style="padding-right: 3em">Pizza</strong> Job Name: <strong>Machine not working</strong> -
          <strong>Todo</strong>
          <a style="float: right; color: #000000;">Priority: <strong>Normal</strong></a> </div>

        <div>
          Created By: <strong>Nangle</strong>
          <a style="float: right; color: #000000;">Assigned To: <strong>Fahon</strong></a>

        </div>
        <div>
          Created On <strong>24/10/2017</strong> Last Updated: <strong>24/10/2017 11:14</strong>
          <a style="float: right; color: #000000;" href="index.php?/job/view/43"><strong>View</strong></a>
        </div>
      </div>
      <div class="containerr collapsed">
        <div class="headerTab"></div>
        <div class="content dashboard-stat blue">
          <p> message </p>
        </div>
      </div>
    </div>
    <div class=" 60 Todo">


      <div class="dashboard-stat green jobsCard" data-assigned="Fahon" data-status="Todo">
        <div>
          Shop Name: <strong style="padding-right: 3em">The Castle</strong> Job Name: <strong>Accounts</strong> -
          <strong>Todo</strong>
          <a style="float: right; color: #000000;">Priority: <strong>Normal</strong></a> </div>

        <div>
          Created By: <strong>Nangle</strong>
          <a style="float: right; color: #000000;">Assigned To: <strong>Nangle</strong></a>

        </div>
        <div>
          Created On <strong>24/10/2017</strong> Last Updated: <strong>24/10/2017 11:19</strong>
          <a style="float: right; color: #000000;" href="index.php?/job/view/44"><strong>View</strong></a>
        </div>
      </div>
      <div class="containerr collapsed">
        <div class="headerTab"></div>
        <div class="content dashboard-stat blue">
          <p> message </p>
        </div>
      </div>
    </div>
    <div class=" 76 Todo">


      <div class="dashboard-stat green jobsCard" data-assigned="Fahon" data-status="Todo">
        <div>
          Shop Name: <strong style="padding-right: 3em">Time</strong> Job Name: <strong>Marketing Visit</strong> -
          <strong>Todo</strong>
          <a style="float: right; color: #000000;">Priority: <strong>Normal</strong></a> </div>

        <div>
          Created By: <strong>Nangle</strong>
          <a style="float: right; color: #000000;">Assigned To: <strong>Fahon</strong></a>

        </div>
        <div>
          Created On <strong>25/10/2017</strong> Last Updated: <strong>25/10/2017 10:32</strong>
          <a style="float: right; color: #000000;" href="index.php?/job/view/51"><strong>View</strong></a>
        </div>
      </div>
      <div class="containerr collapsed">
        <div class="headerTab"></div>
        <div class="content dashboard-stat blue">
          <p> message </p>
        </div>
      </div>
    </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;