各位大侠们:
现在想实现创建数组
如  [{title:'num1'},{title:'num2'},{title:'num3'}]类型的
而num1 是从 action中传过来的一个 List
如何将 List中的某个对象如 员工 em 的属性 name循环添加到 数组中呢?
谢谢!分不多

解决方案 »

  1.   

    你的num2 和 num3 是不是也是从 action 中传过来的
    一般这样的话 你用个 分隔符分割下 然后 拼接成你要的那样

    var  nums='1,2,3';
    var num = nums.split(',');
    var str={};
    str.title = new Array();
    for(var pos =0;pos<num.length;pos++)
    {
        str.title.push(n);
    }
      

  2.   

    当然是把员工 em 的属性 name循环取出然后添加到 数组中啊
      

  3.   

    再说明下:数组中的字段 是 {title:''}形式的
    而 ''中的内容是 从action中传来的list中的 对象的一个字段     var a1 = new Array();
         <%List lands = (List)request.getAttribute("lands");
    for(int i=0;i<lands.size();i++){
    Land la = (Land)lands.get(i);
    %>
    a1[<%=i%>]=[<%{%>title,'<%=la.getName()%>'<%}%>]
    <%
    }%> 
    a1[<%=i%>]=[<%{%>title,'<%=la.getName()%>'<%}%>]
    这里写的好像不对,帮忙看看
      

  4.   

    <script type="text/javascript">
        function check(){
            var param=[{title:"num1"},{title:"num2"},{title:"num3"}];alert(param[0].name);
            for(var i=0;i<param.length;i++){
                param[i].content=document.getElementById(param[i].title).getAttribute("name");
            }
            alert(param[0].name);
        }
    </script>
    <input type=button onclick="check();" value=check>
    <p id=num1 name=n1>s1</p>
    <p id=num2 name=n2>s2</p>
    <p id=num3 name=n3>s3</p>
      

  5.   

    不是很明白,楼主是想把jsp的List转为JS的数组?
      

  6.   


    var a1 = new Array();
    <%
    List lands = (List)request.getAttribute("lands");
    for (int i=0; i<lands.size(); i++) {
    Land la = (Land)lands.get(i);
    %>
    a1.push({'title':'<%=la.getName()%>'});
    <% } %>