简体   繁体   English

数组的最后一个元素为“数组”

[英]Last element of an array comes as an 'Array'

I have following code to get the values of all checked checkboxes. 我有以下代码来获取所有选中的复选框的值。 Surprisingly last element of an array comes as a 'Array'. 令人惊讶的是,数组的最后一个元素是“数组”。

var selected = [];
$('#checkboxes input:checked').each(function(){
  selected.push($(this).attr('value'));
});

Even if only one checkbox is checked, it adds extra element in an array. 即使只选中一个复选框,它也会在数组中添加额外的元素。 Array will be like this: 数组将如下所示:

selected[0]=Dove
selected[1]=Array

What can be the issue with it? 可能是什么问题? I'm unable to find any reason behind this. 我找不到任何背后的原因。 Can anyone help? 有人可以帮忙吗?

HTML Code HTML代码

<ul id='checkboxes' class="list-style1">
 <?php foreach($brands as $row){ ?>
 <span class='checkbox-wrapper' id='<?php echo $brand; ?>'>
 <li><input type='checkbox' value='<?php echo $row['brand']; ?>'>
 <label for='<?php echo $row['brand']; ?>'><?php echo $row['brand']; ?></label>
 </li></span>
 <?php } ?>
</ul>
var checkedValues = $('#checkboxes  input:checked').map(function() {
return this.value;
}).get();

it will return the selected value of the checkbox in array. 它将返回数组中复选框的选定值。

referrence link 推荐连结

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

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