开发者

Read multiple jQuery.val() into array

I have some code that looks like this that works just fine:

var info = [];
for (i = 0; i < 10; i++)
{
     info[i] = $('#info_' + i).val();
}

The problem is that this pattern is very common in my application with some minor variations. What i w开发者_高级运维ould like to do is to make this into a oneliner something like this where info becomes an array:

var info = $('[id^="info_"]').each().val();


Found a solution thanks to Dogbert. All that was missing in his example was the .get()

Here is the solution i ended up using:

var info = $('[id^="info_"]').map(function () { return $(this).val(); }).get();


You can use jQuery.map

var info = $('[id^="info_"]').map(function() { return $(this).val(); } )


$('[id^="info_"]').each(function(){ info.push($(this).val()); });

should do

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜