2017-12-03 126 views
0

我能够使用下面的代码创建一个数组。与此相关的问题是,如果一个人输入50个墨西哥卷饼,该怎么办?恐怕浏览器会遍历整个列表寻找rel属性。如何使用jQuery从输入值创建数组?

<ul id="taco"> 
    <li rel="19">other html goes here</li> 
    <li rel="50">other html goes here</li> 
    <li rel="1535">other html goes here</li> 
    <li rel="875">other html goes here</li> 
</ul> 

$("#taco li").each(function() { 
    $tacoOrder.push($(this).attr('rel')); 
}); 

所以,我正在考虑把taco订单值放在一个输入框中。例如我的代码看起来像这样。

<input value="19, 50, 1535, 875"> 

我想从这个输入中取值并创建一个数组。这甚至是可能的,还是做每个功能更好?

+0

是的,这是可能的。 '$('#tacosValue').val().span(',').map(Number)'用你的选择器替换#tacosValue'。 –

回答

1

这可能是一个解决办法:

var liObj = $("#taco li"); 
//Convert the list to an Array 
var arr = jQuery.makeArray(liObj); 
相关问题