0

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 を入力し、2 番目のボックスに 3000 を入力した場合、これらの値を 5000 まで追加し、それを変数に格納して別のページに渡す方法はありますか? 可能な限り助けてくれてありがとう!

4

1 に答える 1

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 を使用してすべてのコスト入力フィールドを追加し、他の入力非表示フィールドに設定することもできます。

----編集済み----- 以下のように入力要素を変更します

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

フォーム送信イベントで以下の関数を呼び出します。

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

}
</script>

-----編集済み-----

于 2013-05-30T10:03:35.010 に答える