如何在JSP中添加for循环文本框中的值

时间:2013-05-30 09:56:49

标签: jsp

有没有办法在for循环中添加文本框中的值?以下是我的jsp页面中代码的一部分

<% 
 DetailsMod bean = null;
 List resultList = (List) session.getAttribute("list");
 int count1=-1;
 String value2 = "";
 if(resultList.size() > 0 )  {  
  int rowNum = 1;
  for(int i=0; i<resultList.size(); i++){
     bean = (DetailsMod) resultList.get(i);
%>
<input type="text" name="tbx_cost" value="<%=bean.getCost()%>"/>

<% count1 = i;
}
}%>

让我们说resultList返回2作为值,然后会有2个文本框。 如果用户在第一个框中输入2000而在第二个框中输入3000,那么有没有办法将这些值添加到5000并将其存储在变量中以将其传递到另一个页面? 提前感谢您提供任何可能的帮助!

1 个答案:

答案 0 :(得分:0)

    <% 
     DetailsMod bean = null;
     List resultList = (List) session.getAttribute("list");
     int count1=-1,totalCost=0;
     String value2 = "";
     if(resultList.size() > 0 )  {  
      int rowNum = 1;
      for(int i=0; i<resultList.size(); i++){
         bean = (DetailsMod) resultList.get(i);
         totalCost=totalCost+bean.getCost();
    %>
    <input type="text" name="tbx_cost" value="<%=bean.getCost()%>"/>

    <% count1 = i;
    }
    }%>
<input type="hidden" name="total_cost" value="<%=totalCost%>" />

希望这会有所帮助。您也可以使用javascript添加所有成本输入字段并设置为其他输入隐藏字段。

---- ----- EDITED 更改输入元素如下

<input type="text" class="cost" name="tbx_cost" value="<%=bean.getCost()%>"/>
<input type="hidden" id="totalCost" name="total_cost" value="<%=totalCost%>" />

在form-submit事件上调用以下函数。

<script>
function submitForm(){
var total=0;
jQuery('.cost').each(function(){total=total+jQuery(this).val();});
jQuery('#totalCost').val(total);

}
</script>

- - - - - - - - EDITED