简体   繁体   中英

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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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