2013-04-10 87 views
0

对于一个项目,我想实现一个很好的过滤机制与多个复选框。我收到复选框正常工作,以及一个jQuery函数来检查复选框时自动POST表单。选择所有复选框与jQuery形式POST多个复选框

现在我想添加复选框上方的“全选”复选框,但我似乎无法找到正确的方法。我已经尝试了十几种解决方案(有些)类似的问题,但我无法使其正确和一致地工作。

的HTML部分是这样的:

<form action="<?php $_SERVER['PHP_SELF']?>" method="post"> 
    <input type="checkbox" /> Select All colors<br/> 

    <label> <input type="checkbox" name="color[]" value="yellow"> Yellow</label><br/> 
    <label> <input type="checkbox" name="color[]" value="blue"> Blue</label><br/> 
    <label> <input type="checkbox" name="color[]" value="red"> Red</label><br/> 
    <label> <input type="checkbox" name="color[]" value="green"> Green</label><br/><br/> 

    <input type="checkbox" /> Select All brands<br/> 

    <label> <input type="checkbox" name="brand[]" value="Nike"> Nike</label><br/> 
    <label> <input type="checkbox" name="brand[]" value="Adidas"> Adidas</label><br/> 
    <label> <input type="checkbox" name="brand[]" value="SomeBrand"> SomeBrand</label><br/> 
    <label> <input type="checkbox" name="brand[]" value="SomeOtherBrand"> SomeOtherBrand</label><br/> 
</form> 

我用它来发布关于该复选框每次点击的形式jQuery的部分(是不够的):

$('input:checkbox:').live('click',function() { 
    $(this).closest('form').submit(); 
}); 

我现在的问题是什么我需要的jQuery的一部分,以确保这项工作正常? 我希望能够单击标签以取消选择该组中的所有内容,并仅选中该复选框。它还需要POST数组值的形式。最后,如果手动检查所有复选框,则也必须检查“全选”。

希望有人能帮助我,因为我坚持这一长一段时间......

+0

'活()在最新的jQuery版本被删除'。改用'.on('click',handler)'。 – 2013-04-10 17:35:44

+0

你的'<?php $ _SERVER ['PHP_SELF']?>'动作有效吗?如果没有,请尝试'<?php echo $ _SERVER ['PHP_SELF']?>'而不是。只是说。 – 2013-04-10 17:40:28

+0

对不起弗雷德,它有点伪代码,回声在实际的代码中。 也改变了jQuery代码来使用.on('click',handler)。 – Hugo 2013-04-10 17:42:44

回答

0
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript"> 
     $(document).ready(function() 
     { 
      $("#colorall").click(function()    
      { 
       var checked_status = this.checked; 
       $(".color").each(function() 
       { 
        this.checked = checked_status; 
       }); 
      }); 

      $(".color").click(function(){ 
      if($(".color").length == $(".color:checked").length) { 
      document.getElementById("colorall").checked = true; 
      } else { 
      $("#colorall").removeAttr("checked"); 
      }   
      }); 


      $("#brandall").click(function()    
      { 
       var checked_status = this.checked; 
       $(".brand").each(function() 
       { 
        this.checked = checked_status; 
       }); 
      });  

      $(".brand").click(function(){ 
      if($(".brand").length == $(".brand:checked").length) { 
      document.getElementById("brandall").checked = true; 
      } else { 
      $("#brandall").removeAttr("checked"); 
      }   
      }); 

     }); 

     </script> 

<form action="<?php $_SERVER['PHP_SELF']?>" method="post"> 
    <input type="checkbox" id="colorall" /> Select All colors<br/> 

    <label> <input type="checkbox" name="color[]" value="yellow" class="color"> Yellow</label><br/> 
    <label> <input type="checkbox" name="color[]" value="blue" class="color"> Blue</label><br/> 
    <label> <input type="checkbox" name="color[]" value="red" class="color"> Red</label><br/> 
    <label> <input type="checkbox" name="color[]" value="green" class="color"> Green</label><br/><br/> 

    <input type="checkbox" id="brandall"/> Select All brands<br/> 

    <label> <input type="checkbox" name="brand[]" value="Nike" class="brand"> Nike</label><br/> 
    <label> <input type="checkbox" name="brand[]" value="Adidas" class="brand"> Adidas</label><br/> 
    <label> <input type="checkbox" name="brand[]" value="SomeBrand" class="brand"> SomeBrand</label><br/> 
    <label> <input type="checkbox" name="brand[]" value="SomeOtherBrand" class="brand"> SomeOtherBrand</label><br/> 
</form> 
+0

谢谢!这是一个好的开始,但仍然只做2组的checkall功能。我还希望通过单击方式将POST发送到表单。除了手动检查所有复选框之外,还必须检查“全选”。我的观点是,当所有人都被选中时,标签上的点击只能选择特定的复选框。 – Hugo 2013-04-10 17:59:02

+0

我更新了新代码请尝试新代码 – 2013-04-10 18:15:24

+0

它现在效果更好,但是当我选中所有复选框并单击**标签**(并且只有标签)时,我希望所有复选框都取消选中,只有特定的复选框被选中。 此外,POST仍然没有实现我认为。尽管如此,再次感谢! – Hugo 2013-04-10 18:26:50