2015-12-14 19 views

回答

2

您可以使用Array.map()方法返回一个数组出属性:

var items = $('.item').attr('name').split(','), // convert to array 
 
    arr = [].map.call(items, function(value) { // use 
 
    return +value.match(/\d/)[0]; // return the number value from the array "items" 
 
    }); 
 

 
$('pre').html(JSON.stringify(arr));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p name='item1,item2,item3' class='item'>item</p> 
 
<pre></pre>

+0

谢谢你,这完美地为我所需要的。 – HappyCoder

1

获取项目并在其中循环。将每一个添加到您的列表。

var items = []; 
$('.item').each(function(index, value){ 
    //where value is an input 
    items.push(value); 
}); 
1

试试这个$(".items").toArray()得到同样的结果,可能是一个工程。

相关问题