需要帮助,如果没有选中任何复选框,请采取措施

Need help, if none of the checkboxes are checked do something

本文关键字:复选框 任何 采取措施 帮助 如果没有      更新时间:2023-09-26

在我的网站上,当你选中复选框时,"按类别排序"可以正常工作,它会附加每个类别,并隐藏另一个类别的其余部分。

我想做的是,如果没有选中任何复选框,则能够显示所有if类别项目,如果你在网站上单击2个类别并取消选中它们,你会注意到它将为空。

帖子的CSS项是class="rp item"。

,这就是我现在的分类

function categorySort() {      
  $('#CategorySort').attr('firstclick', true);
  $('.cat-sort-toggle').click(function() {
    $('.cat-sort-bar').toggle(500);
  });
  $('.cat-sort-check').change(function() {
    if ( $('#CategorySort').attr('firstclick') == 'true') {
      $('.rp-item').css('display', 'none');
      $('#CategorySort').attr('firstclick', false);
    }
    $objectString = '.' + $(this).attr('showclass');
    if ( $(this).is(':checked') ) {
      $($objectString).show(500);
    } else {
      $($objectString).hide(500);
    }
  });
}
/* the code below was the one i tried to fix the issue,
what it did is it kinda worked but everytime i click the
2nd time on any of the check boxes or just switch
checkboxes it brings everything back instead of only
the posts from the checkbox. Thanks! */ 
else{
  $('.rp-item').css('display', 'inline-block');
  $('#CategorySort').attr('firstclick', true);
} 

您可以检查是否没有选中任何复选框

if($('input[type="checkbox"]:checked').length < 1){
   alert('No one checked');
}

注意:此代码只是一个基本的。。检查课程和ID并在需要的情况下更新此代码

$("input[type='checkbox' class='rp-item']:checked")为您提供了选中的复选框列表。然后你可以把它绑在if:里

if($("input[type='checkbox' class='rp-item']:checked").length > 0){
    // Do Something
}

并在其中编写您的应用程序逻辑。

"无jQuery"方法:

var checkboxes = document.getElementsByClassName("rp-item");
var nothingChecked = true;
for(var i = 0; i < checkboxes.length; i++) {
    if(checkboxes[i].checked == false)
        continue;
    else
        nothingChecked = false;
}
if(nothingChecked == true) {
    // Do Something
}