2012-10-22 18 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

相關問題