简体   繁体   English

淡入/淡入div复选框选择/取消选择

[英]fadein / fadeout div on checkbox select / unselect

I am trying to bring up a menu when any of the checkbox is selected, as you can see in screenshot below, it shows the number of selections and the menu also disappears when none is select. 我试图在选中任何复选框时调出一个菜单,如下面的屏幕快照所示,它显示了选择的数量,并且当没有选择任何菜单时,菜单也消失了。

替代文字

I am able to bring up the menu with this code 我可以用此代码调出菜单

$("input[name='id[]']").focus(function(){
    $("#menu").fadeIn();
});

However, i dont know how to hide it when the checkboxes are unselected and how to count number of selections. 但是,我不知道如何在未选中复选框时隐藏它,以及如何计算选择的数量。

Thank You. 谢谢。

When you check a checkbox, the focus or click event will be triggered. 当您选中一个复选框时,将触发焦点或单击事件。 You can count the checked checkboxes with 您可以使用

$("input:checked").length;

If you use that number to hide your menu it should be possible: 如果使用该数字隐藏菜单,则应该可以:

$("input[name='id[]']").click(function(){
   if($("input:checked").length > 0) { //checked boxes?
      if($("#menu:visible").length == 0) { //menu not visible?
          $("#menu").fadeIn();
      }
   } else {
      $("#menu").fadeOut();
   }
});

To count the number of selections you can use: 要计算选择数量,可以使用:

$("input[type='checkbox']:checked").length;

So.. 所以..

$("input[type='checkbox']").click(function(){
  if ($("input[type='checkbox']:checked").length < 1)
    $("#menu").hide();
});
$("input[name='id[]']").focus(function(){
   if ($(this).is(':checked')){
     $("#menu").fadeIn();
    }else{
     $("#menu").fadeOut();
    }
});

This should do what you need, 这应该做您需要的,

EDIT: 编辑:

Your selector doesn't seem alright though. 您的选择器似乎并不正确。 You should make it more specific. 您应该使其更具体。 for example: 例如:

if it starts with id[ you can do it in this way: 如果以id [开头,您可以通过以下方式进行操作:

$("input[name^=id\\[]")

this will include all input fields their names starting with 'id[' and '\\' this is for escaping this '['. 这将包括所有输入字段,其名称以'id ['和'\\'开头,用于转义'['。

Hope it helps, Sinan. 希望能有所帮助,思南。

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

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