简体   繁体   English

添加/删除多个列表项的类

[英]add/remove class for multiple list items

I have a 'select all' and 'deselect all' button which when clicked should either add or remove the class 'hour-selected' to all list items. 我有一个“全选”和“取消全选”按钮,当单击它们时,应将“按小时选择”类别添加或删除到所有列表项中。 I can't seem to get it to do anything. 我似乎无法做任何事情。

In this example I need to add/remove the class for all li 's within #hour-selected-mon . 在此示例中,我需要为#hour-selected-mon内的所有li添加/删除该类。 dayID returns hour-selected-mon li which I believe is what I want. dayID返回hour-selected-mon li ,我相信这是我想要的。

html example: html范例:

<div id="day1" class="col-md-12 margin-bottom-20">
    <label class="f-13">Select active hours for <b>Monday</b> :</label>
    <div class="clearfix margin-top-10 margin-bottom-10">
        <button type="button" class="btn btn-sm btn-primary hourSelect">Select All</button>
        <button type="button" class="btn btn-sm btn-primary hourDeselect">Deselect All</button>
    </div>
    <ol id="hour-selected-mon" class="hour-select clearfix">
        <li class="<?php echo $hours1['12a'] == 1 ? 'hour-selected' : ''; ?>">12am</li>
        <li class="<?php echo $hours1['1a'] == 1 ? 'hour-selected' : ''; ?>">1am</li>
        <li class="<?php echo $hours1['2a'] == 1 ? 'hour-selected' : ''; ?>">2am</li>
        <li class="<?php echo $hours1['3a'] == 1 ? 'hour-selected' : ''; ?>">3am</li>
        <li class="<?php echo $hours1['4a'] == 1 ? 'hour-selected' : ''; ?>">4am</li>
        <li class="<?php echo $hours1['5a'] == 1 ? 'hour-selected' : ''; ?>">5am</li>
        <li class="<?php echo $hours1['6a'] == 1 ? 'hour-selected' : ''; ?>">6am</li>
        <li class="<?php echo $hours1['7a'] == 1 ? 'hour-selected' : ''; ?>">7am</li>
        <li class="<?php echo $hours1['8a'] == 1 ? 'hour-selected' : ''; ?>">8am</li>
        <li class="<?php echo $hours1['9a'] == 1 ? 'hour-selected' : ''; ?>">9am</li>
        <li class="<?php echo $hours1['10a'] == 1 ? 'hour-selected' : ''; ?>">10am</li>
        <li class="<?php echo $hours1['11a'] == 1 ? 'hour-selected' : ''; ?>">11am</li>
        <li class="<?php echo $hours1['12p'] == 1 ? 'hour-selected' : ''; ?>">12pm</li>
        <li class="<?php echo $hours1['1p'] == 1 ? 'hour-selected' : ''; ?>">1pm</li>
        <li class="<?php echo $hours1['2p'] == 1 ? 'hour-selected' : ''; ?>">2pm</li>
        <li class="<?php echo $hours1['3p'] == 1 ? 'hour-selected' : ''; ?>">3pm</li>
        <li class="<?php echo $hours1['4p'] == 1 ? 'hour-selected' : ''; ?>">4pm</li>
        <li class="<?php echo $hours1['5p'] == 1 ? 'hour-selected' : ''; ?>">5pm</li>
        <li class="<?php echo $hours1['6p'] == 1 ? 'hour-selected' : ''; ?>">6pm</li>
        <li class="<?php echo $hours1['7p'] == 1 ? 'hour-selected' : ''; ?>">7pm</li>
        <li class="<?php echo $hours1['8p'] == 1 ? 'hour-selected' : ''; ?>">8pm</li>
        <li class="<?php echo $hours1['9p'] == 1 ? 'hour-selected' : ''; ?>">9pm</li>
        <li class="<?php echo $hours1['10p'] == 1 ? 'hour-selected' : ''; ?>">10pm</li>
        <li class="<?php echo $hours1['11p'] == 1 ? 'hour-selected' : ''; ?>">11pm</li>
    </ol>
</div>

JS: JS:

//toggle for Select All hour selection
$('.hourSelect').on('click', function() {
    var dayID = $(this).parent().next('ol').attr('id')+' li';
    console.log(dayID);
    $(dayID).addClass('hour-selected');
});     

//toggle for Deselect All hour selection
$('.hourDeselect').on('click', function() {
    var dayID = $(this).parent().next('ol').attr('id')+' li';
    console.log(dayID);
    $(dayID).removeClass('hour-selected');
});

EDIT solution : 编辑解决方案:

//toggle for Select All hour selection
$('.hourSelect').on('click', function() {
    var dayID = $(this).parent().next('ol').attr('id');
    $('#' + dayID + ' li').addClass('hour-selected');
});     

//toggle for Deselect All hour selection
$('.hourDeselect').on('click', function() {
    var dayID = $(this).parent().next('ol').attr('id');
    $('#' + dayID + ' li').removeClass('hour-selected');
});

I was missing the # for the ID value. 我缺少ID值的#号。 Many of you added replies, but none which would work correctly. 你们中的许多人都添加了答复,但没有一个能正常工作。 #hour-selected-mon is in my example, however, there will also be #hour-selected-tue, #hour-selected-wed, .... #hour-selected-sun which is why I am grabbing the ol's ID for use. #hour-selected-mon在我的示例中,但是,还会有#hour-selected-tue,#hour-selected-wed,....#hour-selected-sun,这就是为什么我要获取ol的ID用来。

You can't add or remove class, becouse, you don't put "#" and selector not found. 您不能添加或删除类,因为您不能放入“#”并且找不到选择器。 Without "#" selector look "hour-selected-mon li". 如果没有“#”选择器,请查看“小时选择的星期一”。

$("#" + dayID).removeClass('hour-selected');

Don't use .on 不要使用.on

Uncaught TypeError: Object [object Object] has no method 'on' 

Just use click. 只需点击一下即可。 http://jsfiddle.net/H73VU/ http://jsfiddle.net/H73VU/

Isn't a point or some sign at the very beginning of your string missing? 字符串开头是否缺少点或符号? If you want to select all these classes, you should add ".". 如果要选择所有这些类,则应添加“。”。

Use this in your select all function: 在全选功能中使用此功能:

$('.hour-select > li').toggleClass('hour-selected', true);

And in your deselect, change true to false. 然后取消选择,将true更改为false。

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

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