日期:2014-05-16  浏览次数:20431 次

如何jquery each()方法遍历组成新的数组
通过jquery的$.each()方法遍历一个对象并取其ID重新拼装成一个数组如何操作?
比如遍历select对象,并取其值:
<select id="test1">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
</select>

然后拼装成{1,2,3,4}这样的数组,或者json对象

------解决方案--------------------
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
var glob_oplist = new Array();
$(document).ready(function() {
$('#test1').each(
                                function() {
                                        $(this).children("option").each(
                                                        function() {
                                                                glob_oplist.push($(this).text())
                                                        }
                                                )
                                }
                        )
                        alert(JSON.stringify(glob_oplist))
}
)
</script>
</head>

<body>
<select id="test1">
  <option>1</option>
  <option>2</option>
  <option>3</option>
<option>4</option>
</select>

</body>
</html>

------解决方案--------------------
HTML code
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>   
<script>
$(document).ready(function(){
     var ar=new Array();
       $("#test1").children("option").each(function(){
            ar.push($(this).text());
           
        });
    alert(ar);
});

</script>
<select id="test1">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
</select>