简体   繁体   English

在Struts中使用javascript添加到数组

[英]add to array with javascript in struts

OK I have this in a jsp page 好的,我在jsp页面中有这个

<div id="productList">  
  <logic:iterate id="product" indexId="aid" name="TeamMaintenanceForm" property="team.productList">                             
          <div id='<bean:write name="product" property="id" />' >
                <bean:write name="product" property="name" /> 
          </div>        
        </logic:iterate>                                
</div>  

Now I would like to add an object to this array with javascript something like 现在,我想使用javascript将对象添加到此数组中,例如

  function addProduct(){
       var object;
       object.name='newProduct';
       object.id=5;
       productList.add(newProduct);
}

SO that the new object shows up with the others in the array and so that when i do submit this page it gets submited to the form with the objects in the array. 这样,新对象将与数组中的其他对象一起显示,因此当我提交此页面时,它将与数组中的对象一起提交给表单。

Is there an easy way to do this? 是否有捷径可寻?

  function addProduct(){
    var object;
    object.name='newProduct';
    object.id=5;

    // get the reference of div (productList)
    var productList = document.getElementById('productList')  ;

    // create the div structure of children
    var newProductDiv = "<div id='"+object.id +"' />'"+object.name + "</div>"; 

    // append the children to the productList div.
    productList.appendChild(newProductDiv);
 }

you can make it parameterized 您可以将其参数化

function addProduct(object){

    // get the reference of div (productList)
    var productList = document.getElementById('productList')  ;

    // create the div structure of children
    var newProductDiv = "<div id='"+object.id +"' />'"+object.name + "</div>"; 

    // append the children to the productList div.
    productList.appendChild(newProductDiv);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM