Read multiple jQuery.val() into array

后端 未结 3 414
臣服心动
臣服心动 2021-02-04 03:47

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();
}
相关标签:
3条回答
  • 2021-02-04 04:21
    $('[id^="info_"]').each(function(){ info.push($(this).val()); });
    

    should do

    0 讨论(0)
  • 2021-02-04 04:31

    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();
    
    0 讨论(0)
  • 2021-02-04 04:46

    You can use jQuery.map

    var info = $('[id^="info_"]').map(function() { return $(this).val(); } )
    
    0 讨论(0)
提交回复
热议问题