javascript jquery复选框,如何获取所有选定的复选框并将它们添加到数组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9688521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:28:19  来源:igfitidea点击:

jquery checkboxes, how to get all selected checkboxes and add them to array?

javascriptjquery

提问by Zoran

Hi I have the following page:

嗨,我有以下页面:

<input type="checkbox"  name="fruit1" id="1"  class="box">Banana<br /><br />
<input type="checkbox"  name="fruit2" id="2"  class="box">Cherry<br /><br />
<input type="checkbox"  name="fruit3" id="3"  class="box">Strawberry<br /><br />
<input type="checkbox"  name="fruit4" id="4"  class="box">Orange<br /><br />
<input type="checkbox"  name="fruit5" id="5"  class="box">Peach<br /><br />
<input type="button" id="groupdelete" value="clickme"><br />

 $(document).ready(function(){

$('#groupdelete').on('click', function(){
  var names = [];
   $('input:checked').each(function() {

       names.push($('input:checked').attr("name") + $('input:checked').attr('id'));

 });
   console.log(names); 
})

})

What I am trying to do is the following:

我正在尝试做的是以下内容:

To add the checked checkboxes in the array. And after that, I would like to be able to pass the value in php variable.

在数组中添加选中的复选框。之后,我希望能够在 php 变量中传递值。

When I excecute the code now, I am getting result like this:

当我现在执行代码时,我得到这样的结果:

["fruit22", "fruit22", "fruit22"]

[“fruit22”、“fruit22”、“fruit22”]

Any help will be deeply appreciated.

任何帮助将不胜感激。

Regards, Zoreli

问候, 佐雷利

回答by Anthony Grist

You need to use thisrather than 'input:checked'inside the .each()function to refer to the current element in the set being examined. If you re-use the selector you're getting the set again, then only ever getting the attributes from the firstelement in the set.

您需要使用this而不是'input:checked'.each()函数内部来引用正在检查的集合中的当前元素。如果您重新使用选择器,您将再次获得集合,那么只会从集合中的第一个元素中获取属性。

$('input:checked').each(function() {
    names.push($(this).attr("name") + this.id);
});

回答by Hamza Waqas

Change your html to

将您的 html 更改为

<input type="checkbox"  name="fruits[]" id="1"  class="box">Banana<br /><br />
<input type="checkbox"  name="fruits[]" id="2"  class="box">Cherry<br /><br />
<input type="checkbox"  name="fruits[]" id="3"  class="box">Strawberry<br /><br />
<input type="checkbox"  name="fruits[]" id="4"  class="box">Orange<br /><br />
<input type="checkbox"  name="fruits[]" id="5"  class="box">Peach<br /><br />
<input type="button" id="groupdelete" value="clickme"><br />

And now, look to jQuery/Javascript

现在,看看 jQuery/Javascript

$(document).ready(function(){
    $('#groupdelete').click(function() {
        var marked = new Array();
        var k = 0;
        $('input:checked').each(function(index,value) {
            marked[k] = value;
            k++;
        });        
        alert(marked[0].id);
    });
});

alert is just giving you the demo by accessing the direct access on the array's index.

alert 只是通过访问对数组索引的直接访问来为您提供演示。