简体   繁体   English

使用复选框过滤w。 jQuery的

[英]Filter Using Checkboxes w. Jquery

I am trying to create checkbox filters that show or hide boxes on my pagebased on their id. 我正在尝试创建复选框过滤器,以根据其ID在页面上显示或隐藏框。 I'm very new to jquery, but have been playing around with this for a while and cannot seem to get the boxes to hide / show. 我对jquery还是很陌生,但是已经玩了一段时间,似乎无法隐藏/显示框。 Please see my code below. 请在下面查看我的代码。 Thanks! 谢谢!

Sport model 运动模型

class Sport(models.Model):
    name = models.CharField(max_length=128)

Snippet of HTML to filter 要过滤的HTML代码段

{% for game in game_list %}
                            {% if game.sport = sport %}
                                <div class="col-xs-12 col-sm-12 col-lg-12">
                                    <section id="{{ sport.name }}" class="box pick-game">
                                        <div class="box box-content">
                                            <div class="game-bet-header">
                                                <div class="row">
                                                    <div class="col-md-3">
                                                         <h4 class="game-date"> {{ game.time|time }} - {{ game.time|date:"M-d" }}</h4>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <h4 class="game-title">{{ game.team1 }} vs {{ game.team2 }} </h4>
                                                    </div>
                                                </div>
                                            </div>

HTML Checkboxes to Use as Filters HTML复选框用作过滤器

<div class="sport-sidebar">
            <section class="sports-box">
                {% for sport in sports %}
                    <div id="sport-filter" class="sport-name">
                        <label for="filter-{{ sport.name }}"><input type="checkbox" class="sport" value="{{ sport.name }}" id="filter-{{ sport.name }}">{{ sport.name }}</label>
                    </div>
                {% endfor %}
            </section>
</div>

Jquery jQuery的

$('#sport-filter input:checkbox').click(function() {
    $('#sport-filter input:checkbox:checked').each(function() {
        $("#" +$(this).val()).show()
    });
});

Please help provide suggestions to solve this problem! 请帮助提供解决此问题的建议! Thanks so much 非常感谢

Updated Javascript on 9/20, but still not working 在9/20更新了Javascript,但仍然无法正常工作

Please see below for updated javascript. 请参阅下面的更新的javascript。 Initializing with .hide() is not working, but if I manually set the id of each sport to display: none in the CSS it does hide. 使用.hide()进行初始化无法正常工作,但是如果我手动设置要显示的每种运动的ID:CSS中的任何一项都不会隐藏。 This is leading me to believe that either .show and .hide are not working, or they are not correctly capturing my class. 这使我相信.show和.hide无法正常工作,或者它们无法正确捕获我的课程。 I also added a class to my checkbox in the javascript to distinguish it from the others. 我还在javascript的复选框中添加了一个类,以区别于其他类。

$("#mlb-pick").hide();
$("#nfl-pick").hide();


$(".sport-sidebar input[type=checkbox]").on('click', function() {
  var thisval =  $(this).val();
  if ($(this).prop('checked')){
      $('#' + thisval+"-pick").show();
    }
  else{
    $('#' + thisval).hide();
    }

});

Here's an example of how .each works in jquery 这是.each在jquery中如何工作的示例

 function toggleChks() { $("input[type=checkbox]").each(function(index, item) { console.log(index, item); if ($(item).prop('checked')) $(item).prop('checked', false); else $(item).prop('checked', true); }) } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick="toggleChks()">toggle</button> <input type="checkbox" checked value="1"> <input type="checkbox" checked value="2"> <input type="checkbox" checked value="3"> <input type="checkbox" value="4"> <input type="checkbox" value="5"> <input type="checkbox" value="6"> 

Here's a working one with your example, you've had to put the "id" for the sections, or in other case, change the jquery selector for it's class. 这是一个适合您的示例的示例,您必须在各节中放置“ id”,否则,请更改其类的jquery选择器。

Also you have to set the click action for the input checkbox elements. 另外,您还必须为输入复选框元素设置单击动作。

Hope this helps. 希望这可以帮助。

 //initialize state $("#mlb").hide(); $("#nfl").hide(); $("input[type=checkbox]").on('click', function() { var thisval = $(this).val(); if ($(this).prop('checked')){ $('#' + thisval).show(); } else{ $('#' + thisval).hide(); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="sport-filter" class="mlb"> <label for="mlb-filter"><input type="checkbox" value="mlb" id="mlb-filter">mlb</label> </div> <div id="sport-filter" class="nfl"> <label for="nfl-filter"><input type="checkbox" value="nfl" id="nfl-filter">nfl</label> </div> <section class=mlb id="mlb"> <p> Hey </p> </section> <section class=nfl id="nfl"> <p> Hey 2 </p> </section> 

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

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