2013-03-09 46 views
0

我有一個窗體中的隱藏字段,其值由javascript設置。我已經確認價值的確定了。但是,當我嘗試將它發送給一個servlet時,它會生成一個空值。我確實使用POST方法和「提交」按鈕提交給servlet。該值由用戶在「qtyText」文本框中輸入獲得。在JS中,它的值被動態設置。爲什麼它不會被髮送到servlet? JSJSP隱藏字段在servlet中產生null

function(calculateTotalPrice(txtbxvalue, price) 
    { 
    myForm = document.forms[0];   
     var txtBx = myForm.elements['qtyText']; 
     var txtBxHidden = myForm.elements['qtyTextHidden']; 
     for(var i = 0; i < txtBx.length; i++) 
     { 
      var curTxtBx = txtBx[i].value; 
      var txtBxHiddenBx = txtBxHidden[i]; 
      txtBxHiddenBx.value = curTxtBx; 
     } 
    } 

HTML

<table> 

    <c:forEach items="${ListInServlet}" var= "exBean"> 
    <form name = "tableForm" method = "post" action= "/rpsapp/someservlet"> 
    <input type="hidden" name="productId" value= "<c:out Value 
              = "${exBean.productId}"/>" /> 
     <input type="hidden" value = "somevalue" name="qtyTextHidden"/> 
     <input name = "qtyText" type = "textbox" size = "2" value = "" onChange 
           = "calculateTotalPrice(this, '${exBean.price}')"/> 

    </c:forEach> 
    </form> 
</table> 
+0

如何檢索servlet中的值? – niculare 2013-03-09 14:32:44

+0

String qty = request.getParameter(「qtyTextHidden」); – Raghu 2013-03-09 14:36:18

回答

0

你的foreach語句(標籤)與表單標籤重疊。

您需要要麼有以下格式:

<c:forEach ...> <form ...> ... </form> </c:forEach> 
+0

我們如何糾正?我知道它並不完美。那裏有另一個提交按鈕。但是這是否會導致值不會傳遞給servlet?還有其他兩個值(一個是隱藏的,另一個是「select」,這個值是沒有問題的。) – Raghu 2013-03-09 14:45:16

+0

我不確定你在做什麼,但是你的表單打開/關閉標籤應該匹配,並且你的提交按鈕應該在表單內 – jonasnas 2013-03-09 14:48:40

+0

thx的建議,但它似乎並沒有工作 – Raghu 2013-03-09 15:08:11

0

你必須創建爲你的產品隱藏變量很多。

<form name = "tableForm" method = "post" action= "/rpsapp/someservlet"> 
    <c:forEach items="${ListInServlet}" var= "exBean"> 
    <input type="hidden" name="productId${exBean.productId}" value= "<c:out Value 
              = "${exBean.productId}"/>" /> 
     <input type="hidden" value = "somevalue" name="qtyTextHidden${exBean.productId}"/> 
     <input name = "qtyText${exBean.productId}" type = "textbox" size = "2" value = "" onChange 
           = "calculateTotalPrice(this, '${exBean.price}')"/> 

    </c:forEach> 
    </form> 
+0

問題是,只有用戶輸入的值沒有被提交的隱藏字段,但其他字段有。我有整個鱈魚在這裏JSFiddle jsfiddle.net/raghujoshi/yBJJJ – Raghu 2013-03-09 20:38:05