jQuery获取每个div的子子div并将信息获取到数组中 [英] Jquery get each div's sub child divs and grab info into an array
问题描述
我有一些看起来像这样的html
I have a some html that looks like this
<div id="main">
<div id="sub_main_1" class="sub_main">
<input type="text" class="sub_name_first" /><br />
<input type="text" class="sub_name_second" /><br />
</div>
<div id="sub_main_2" class="sub_main">
<input type="text" class="sub_name_first" /><br />
<input type="text" class="sub_name_second" /><br />
</div>
</div>
我想将每个sub_main divs信息提取到javascript数组中.到目前为止,我已经将此作为我的jquery代码
I would like to pull out each sub_main divs information into an array in javascript. So far I have this as my jquery code
$('#main').find('.sub_main').each(
function() {
alert('hi');
});
警报只是一个测试,它应该两次显示"hi".但这是行不通的.我也不清楚如何将两个输入存储在javascript数组中.任何帮助将是巨大的!谢谢,
The alert is just a test that it should show "hi" twice. But this is not working. I am also not clear on how I can store the two inputs in a javascript array. Any help would be great! Thanks,
推荐答案
var array = $('#main input').map(function() {
return $(this).val();
}).get();
请注意,这将返回#main
下所有input
元素的值.如果不需要所有input
元素,则可以根据需要使$('#main input')
选择器具体化.
Note that this will return the values of all input
elements under #main
. You can make the $('#main input')
selector as specific as you need if not all input
elements are desired.
这篇关于jQuery获取每个div的子子div并将信息获取到数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!