2016-10-08 98 views
0

HTML代码:如何使用jQuery获取所有选中复选框的值?

<form method="post" id="cate-form"> 
      <ul class="expander-list" id="category"> 
       <li> 
       <div class="radio" style="padding-left:0px"> 
        <label> 
        <input type="checkbox" id="all" value="all"> 
        All </label> 
       </div> 
       </li> 
       <li> 
       <div class="radio" style="padding-left:0px"> 
        <label> 
        <input type="checkbox" name="filter[]" value="active"> 
        Active </label> 
       </div> 
       </li> 
       <li> 
       <div class="radio" style="padding-left:0px"> 
        <label> 
        <input type="checkbox" name="filter[]" value="inactive"> 
        Inactive </label> 
       </div> 
       </li> 
      </ul> 
      </form> 

jQuery代码:

<script> 
     $(document).ready(function(){ 
      $("#all").click(function() { 
       $(':checkbox').prop('checked', this.checked); 
      }); 
      $(":checkbox").change(function(){ 
       var checked = []; 
       alert("it running"); 
       $('input[name=checkbox] :checked').each(function(){ 
        alert("data is pushing inside checked array"); 
        checked.push($(this).val()); 
       }) 
      }) 
     }); 
     </script> 

通过点击所有复选框它选择所有复选框。但我怎样才能得到一个所有选定的复选框的价值。我在谷歌搜索具体这个查询,但没有什么帮助,现在请帮助我解决这个问题,并提前感谢。

+1

的可能的复制[如何在jQuery中获取复选框值(http://stackoverflow.com/ question/786142/how-to-retrieve-checkboxes-values-in-jquery) – Mohammad

回答

0

变化

$('input[name=checkbox] :checked') 

$('input[type=checkbox]:checked') 

因为你的复选框实际上命名filter[]但它们的类型为复选框。 你的代码应该工作,然后

+0

仍然是,每个函数都不起作用 –

+0

你是对的 - 那是因为':checked'之前的空间现在尝试 –

0

如果你想在阵列中的所有值,那么试试这个: -

var checkedValues = $('input:checkbox:checked').map(function() { 
    return this.value; 
}).get(); 
相关问题