2013-04-18 24 views
1

我想,當我點擊添加它來獲得這個值如何動態添加值傳遞到servlet的

名稱=「產品‘+專櫃+’」

它是一個動態值會創造另一個行,任何人都可以告訴我如何獲得這個動態值,我應該如何傳遞給servlet,以及如何獲得servlet。 我使用表單提交:JSP和JAVA環境。

$("#addrow").on("click", function() { 
     counter++; 

     var newRow = $("<tr>"); 
     var cols = ""; 
     cols += '<td><input type="text" name="product' + counter + '"/></td>'; 
     cols += '<td><input type="text" name="price' + counter + '"/></td>'; 
     cols += '<td><input type="text" name="qty' + counter + '"/></td>'; 
     cols += '<td><input type="text" name="linetotal' + counter + '" readonly="readonly"/></td>'; 
     cols += '<td><a class="deleteRow"> x </a></td>'; 
     newRow.append(cols); 
     alert(cols); 
     $("table.order-list").append(newRow); 
    }); 
+0

這是什麼都與Java和JSP做什麼?它看起來像一個JavaScript問題。 – 2013-04-18 07:17:49

回答

2

無需添加計數器,就可以像這樣更改代碼。

$("#addrow").on("click", function() { 
     counter++; 

     var newRow = $("<tr>"); 
     var cols = ""; 
     cols += '<td><input type="text" name="product"/></td>'; 
     cols += '<td><input type="text" name="price"/></td>'; 
     cols += '<td><input type="text" name="qty"/></td>'; 
     cols += '<td><input type="text" name="linetotal" readonly="readonly"/></td>'; 
     cols += '<td><a class="deleteRow"> x </a></td>'; 
     newRow.append(cols); 
     alert(cols); 
     $("table.order-list").append(newRow); 
    }); 

而且在servlet讓每個input box如下面的代碼鏈接的所有valuesarray

String[] products = request.getParameterValues("product"); 
String[] prices= request.getParameterValues("price"); 
String[] qtys= request.getParameterValues("qty"); 
String[] linetotals = request.getParameterValues("linetotal"); 

編輯添加完整的代碼

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script> 


</head> 
<body> 
<form id="validationform" name="validationform" method="get" action="BG_Form"> 

<table class="order-list" style="margin-left: 228px;"> 
    <thead> 
     <tr><td>Product</td><td>Price</td><td>Qty</td><td>Total</td></tr> 
    </thead> 

    <tbody> 
     <tr> 
      <td><input type="text" name="product"></td> 
      <td><input type="text" name="price"> 
      </td><td><input type="text" name="qty"></td> 
      <td><input type="text" readonly="readonly" name="linetotal"></td> 
      <td><a class="deleteRow"> x </a></td> 
     </tr> 
    </tbody> 

    <tfoot> 
     <tr> 
      <td style="text-align: center;" colspan="5"> 
       <input type="button" value="Add Product" name="addrow" id="addrow"> 

      </td> 
     </tr> 

     <tr> 
      <td colspan="5"> 
       Grand Total: Rs<span id="grandtotal"></span> 
      </td> 
     </tr> 
     <tr> 
     <td> 
     In Words <span id="inworDs" ></span> 

     </tr> 
     <tr> 
       <td> 
       <input type="submit"/> 

     </tr> 
    </tfoot> 
</table> 

<form> 



<script> 
    $("#addrow").click(function(){ 
     var newRow = $("<tr>"); 
       var cols = ""; 
       cols += '<td><input type="text" name="product"/></td>'; 
       cols += '<td><input type="text" name="price"/></td>'; 
       cols += '<td><input type="text" name="qty"/></td>'; 
       cols += '<td><input type="text" name="linetotal" readonly="readonly"/></td>'; 
       cols += '<td><a class="deleteRow"> x </a></td>'; 
       newRow.append(cols); 
     $("table.order-list").append(newRow); 
    }); 

</script> 
</body> 
</html> 
+0

謝謝,但我只獲得第一個價值。我使用這個(字符串字符串:產品){ \t \t System.out.println(「Hii Product」+ string); \t \t \t}告訴我任何其他方式來迭代。再次感謝 – 2013-04-18 08:41:04

+0

讓我明白你在'ui'上添加了多於一行並且在服務器端只獲得一個值。如果是這種情況,請添加您的整個jsp代碼。 – 2013-04-18 08:47:56

+0

將您的表單方法更改爲'GET''

'並檢查鍵'product'的值是什麼 – 2013-04-18 08:49:46