jQuery获取每个div的子子div并将信息获取到数组中 [英] Jquery get each div's sub child divs and grab info into an array

查看:63
本文介绍了jQuery获取每个div的子子div并将信息获取到数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些看起来像这样的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆