简体   繁体   English

如何通过使用jQuery获取所有选定复选框的值?

[英]How to get value of all selected checkbox by using jQuery?

HTML code: HTML代码:

<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 Code: 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>

By clicking on all checkbox it selecting all checkboxes. 通过单击所有复选框,可以选择所有复选框。 but How can I get a value of an all selected checkboxes at a time.I searched on google for specific this query but nothing helpful is found now please help me for solving a this issue and thanks in advance. 但是我如何一次获得所有选定复选框的值。我在google上搜索了此查询,但是没有找到任何有用的信息,请立即帮助我解决此问题,在此先感谢。

change 更改

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

to

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

since your checkboxes are actually named filter[] but their type is checkbox. 因为您的复选框实际上被命名为filter[]但是它们的类型是checkbox。 your code should work then 您的代码应该可以正常工作

if you want all value in an array then try this:- 如果您想要数组中的所有值,请尝试以下操作:-

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

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

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