本文介绍了将多个 jQuery.val() 读入数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些看起来像这样可以正常工作的代码:
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 would like to do is to make this into a oneliner something like this where info becomes an array:
var info = $('[id^="info_"]').each().val();
推荐答案
感谢 Dogbert.他的示例中缺少的只是 .get()
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();
这篇关于将多个 jQuery.val() 读入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!