Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将多个复选框标签放入数组_Javascript_Jquery_Arrays_Checkbox - Fatal编程技术网

Javascript 将多个复选框标签放入数组

Javascript 将多个复选框标签放入数组,javascript,jquery,arrays,checkbox,Javascript,Jquery,Arrays,Checkbox,我希望将表单中所有选中的复选框放入一个数组中 这就是我所做的 $(“div.category-panel输入:选中”).next('label').text()完美地获得所有选中的复选框,但它只是将它们显示为一个文本。例如,checkbox1、checkbox2和checkbox3(如果选中)将显示为checkbox1checkbox2checkbox3 我希望把这些不同的复选框放在一个数组中,这样我就可以使用它们了 $('.submit').on("click", function(e

我希望将表单中所有选中的复选框放入一个数组中

这就是我所做的

$(“div.category-panel输入:选中”).next('label').text()完美地获得所有选中的复选框,但它只是将它们显示为一个文本。例如,
checkbox1
checkbox2
checkbox3
(如果选中)将显示为
checkbox1checkbox2checkbox3

我希望把这些不同的复选框放在一个数组中,这样我就可以使用它们了

    $('.submit').on("click", function(e) {

    //got all checked checkboxes into 'children'.            
    children = $("div.category-panel input:checked").next ('label').text();

    //Put in array.            
    var array = [];
    var i = 0;
    $.each(children, function(key, value){
        array.push($(this).text());
    });

    //Show the array.
    for (var i = 0; i < array.length; i++) {
      console.log(array[i]);
    }

 });
$('.submit')。在(“单击”上,函数(e){
//将所有选中的复选框放入“children”。
children=$(“div.category-panel输入:选中”).next('label').text();
//摆成一排。
var数组=[];
var i=0;
$.each(子项、函数(键、值){
array.push($(this.text());
});
//显示数组。
对于(var i=0;i
HTML,以防万一是:-

<div class="category-panel">

<input type="checkbox" name="name_service2" id="tid-46" value="46" checked="checked"> <label class="option" for="edit-tid-46">CheckBox1</label>

<input type="checkbox" name="name_service3" id="tid-47" value="47" checked="checked"> <label class="option" for="edit-tid-47">CheckBox2</label>

<input type="checkbox" name="name_service4" id="tid-44" value="44" checked="checked"> <label class="option" for="edit-tid-44">CheckBox3</label>

<input type="checkbox" name="name_service5" id="tid-48" value="48" checked="checked"> <label class="option" for="edit-tid-48">CheckBox4</label>

</div>

复选框1
复选框2
复选框3
复选框4
您可以像

$('.submit')。在(“单击”上,函数(e){
//将所有选中的复选框放入“children”。
var数组=$(“div.category-panel输入:选中”).next('label').map(函数(){
返回$(this.text();
}).get();
//显示数组。
对于(var i=0;i
$('.submit')。在(“单击”上,函数(e){
//将所有选中的复选框放入“children”。
var数组=$(“div.category-panel输入:选中”).next('label').map(函数(){
返回$(this.text();
}).get();
//显示数组。
对于(var i=0;i

复选框1
复选框2
复选框3
复选框4
提交
您可以使用

$('.submit')。在(“单击”上,函数(e){
//将所有选中的复选框放入“children”。
var数组=$(“div.category-panel输入:选中”).next('label').map(函数(){
返回$(this.text();
}).get();
//显示数组。
对于(var i=0;i
$('.submit')。在(“单击”上,函数(e){
//将所有选中的复选框放入“children”。
var数组=$(“div.category-panel输入:选中”).next('label').map(函数(){
返回$(this.text();
}).get();
//显示数组。
对于(var i=0;i

复选框1
复选框2
复选框3
复选框4
提交
您需要使用
.map()
函数将所有标签文本作为对象。然后使用
.get()
将它们放入数组:

$("div.category-panel input:checked").next('label').map(function(){
    return $(this).text();
}).get();// ["checkbox1","checkbox2" ,"checkbox3"]
如果要将它们作为逗号分隔的值,请在
.get()之后使用
.join()

您需要使用
.map()
函数将所有标签文本作为对象获取。然后使用
.get()
将它们放入数组:

$("div.category-panel input:checked").next('label').map(function(){
    return $(this).text();
}).get();// ["checkbox1","checkbox2" ,"checkbox3"]
如果要将它们作为逗号分隔的值,请在
.get()之后使用
.join()

$("div.category-panel input:checked").next('label').map(function(){
  return $(this).text();
}).get().join();//  "checkbox1, checkbox2 ,checkbox3"
   <input type="checkbox" name="name_service2" id="tid-46" value="46" checked="checked" /> <label class="option" for="edit-tid-46">A Value</label>

   <input type="checkbox" name="name_service3" id="tid-47" value="47" checked="checked" /> <label class="option" for="edit-tid-47">A Value</label>

   <input type="checkbox" name="name_service4" id="tid-44" value="44" checked="checked" /> <label class="option" for="edit-tid-44">A Value</label>

   <input type="checkbox" name="name_service5" id="tid-48" value="48" checked="checked" /> <label class="option" for="edit-tid-48">A Value</label>
$('.submit').on("click", function(e) {
    //got all checked checkboxes into 'children'.            
    children = $("div.category-panel input:checked").next ('label');

    //Put in array.            
    var array = [];
    $.each(children, function(){
        array.push($(this).text());
    });

    //Show the array.
    $(array).each(function(){
        console.log(this.toString());
    });

 });