2012-10-22 20 views
0

我正在购买虚拟物品的市场上工作,并计划让它显示每行中的每个物品的详细信息,然后点击“购买”按钮到每个项目。下面是我有:如何在Struts 2中提交具体的迭代器入门行为

<s:form method="post"> 
    <s:hidden name="shownType"/> 
    <table> 
     <tr> 
      <td>Name</td> 
      <td>Image</td> 
      <td>Type</td> 
      <td>Price</td> 
      <td>Buy</td> 
     </tr> 
     <s:iterator value="items" var="item"> 
      <tr> 
       <td><s:property value="name" /></td> 
       <td><s:property value="image" /></td> 
       <td><s:property value="type" /></td> 
       <td align="center"><s:property value="price" /></td> 
       <td><s:submit value="Buy" 
       onclick="coinAlert()" action="Buy" 
       type="button" theme="simple"> 
       </s:submit></td> 
      </tr> 
     </s:iterator> 
    </table> 
</s:form> 

从理论上讲,我想具体的项目对象(姓名,图像等)传递给购买行动(具体到名为boughtItem的项目)。问题是,我不确定如何引用点击按钮旁边的特定项目。我试过了

<s:hidden name="boughtItem.name" value="%{#item.name}"/> 

但是它将buyingItem的名称设置为List中所有项目的名称,用逗号分隔。任何尝试使用迭代器一次设置整个buyingItem对象失败。

任何有关这方面的意见将不胜感激。

回答

0

而不是使用submit按钮使用一个简单的按钮,使用JavaScript

<s:form method="post" action="Buy" name="myForm"> 
    <s:hidden name="name" id='name_to_submit'/> 
    <s:hidden name="image" id='image_to_submit'/> 
    <s:hidden name="type" id='type_to_submit'/> 
    <s:hidden name="price" id='price_to_submit'/> 
    <table> 
     <tr> 
      <td>Name</td> 
      <td>Image</td> 
      <td>Type</td> 
      <td>Price</td> 
      <td>Buy</td> 
     </tr> 
     <s:iterator value="items" var="item"> 
      <tr id="<s:property value="name" />"> 
       <td><s:property value="name" /></td> 
       <td class='image_td'><s:property value="image" /></td> 
       <td class='type_td'><s:property value="type" /></td> 
       <td class='price_td' align="center"><s:property value="price" /></td> 
       <td><input type="button" value="Buy" 
       onclick="submitForm('<s:property value="name" />')"> 
       </td> 
      </tr> 
     </s:iterator> 
    </table> 
</s:form> 

提交表单的JavaScript

function submitForm(name){ 
    //All this would be very easy if you were using jQuery 
    var nameElem = document.getElementById(name); 
    //get values for selected row 
    var imageVal = nameElem.getElementsByClassName("image_td")[0].innerHTML; 
    var priceVal = nameElem.getElementsByClassName("price_td")[0].innerHTML; 
    var typeVal = nameElem.getElementsByClassName("type_td")[0].innerHTML; 
    //set the values to submit 
    document.getElementById("name_to_submit").value = name; 
    document.getElementById("image_to_submit").value = imageVal; 
    document.getElementById("price_to_submit").value = priceVal; 
    document.getElementById("type_to_submit").value = typeVal; 

    document.myForm.submit(); //finally submit the form 
} 

的这将是更好地使用一些标识属性作为ID不name

+0

工作就像一个魅力!谢谢! – kramsay

相关问题