0

好的,我在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>  

现在我想用javascript向这个数组添加一个对象,比如

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

因此,新对象与数组中的其他对象一起显示,因此当我提交此页面时,它会与数组中的对象一起提交到表单。

是否有捷径可寻?

4

1 回答 1

0
  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);
 }

你可以让它参数化

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);
}
于 2013-10-08T13:28:43.380 回答