简体   繁体   English

从Servlet获取复选框值

[英]Getting checkbox value(s) from a Servlet

I have a servlet with MySQL database in it. 我有一个带有MySQL数据库的servlet。 It looks like this: 它看起来像这样: 在此输入图像描述

here is the piece of code for it: 这是它的代码片段:

out.println("<table id = \"main_section\" cellspacing=\"1\" bgcolor=\"red\" > ");
out.println ("<tr> ");
out.println("<td >NUMBER</td>");
out.println("<td >PAYMENT</td>");
out.println("<td >RECEIVER</td>");
out.println("<td >VALUE </td>");
out.println("<td >CHECKBOX</td>");
out.println("</tr>");
out.println("<tr>");
for (int i = 0; i < ex.getExpenses().size(); i++) {
    out.println("<td > " + ex.getExpenses().get(i) + "</td>");

    if (i>0 && (i+1)%4==0) {
        out.println("<td><input type=\"checkbox\" name=\"checkbox\"></td>");
        out.println("</tr><tr>");

    }     
}
out.println("</tr>");

What I need to do is to create a submit button that calculates the sum of VALUE of the checked boxes. 我需要做的是创建一个提交按钮,用于计算复选框的VALUE总和。 for instance, if NUMBER 1 and 2 are checked the submit button should give the result of 5577.0 (VALUE 22.0+5555.0). 例如,如果选中了NUMBER 1和2,则提交按钮的结果应为5577.0(VALUE 22.0 + 5555.0)。 Can anyone please help me with that? 任何人都可以帮助我吗?

First of all, you should learn about JSPs and generate your HTML markup from a JSP rather than from the servlet. 首先,您应该了解JSP并从JSP而不是从servlet生成HTML标记。

Now for your problem. 现在为你的问题。 Each of these rows comes from a database table. 这些行中的每一行都来自数据库表。 So each of these rows should have an ID (primary key). 所以这些行中的每一行都应该有一个ID(主键)。 Assign the ID of the row to the value of the checkbox. 将行的ID分配给复选框的值。 When you submit your form, the servlet will receive all the IDs of the checked checkboxes. 提交表单时,servlet将收到已选中复选框的所有ID。 Get the values corresponding from these IDs from the database, and sum them (or execute a query that computes the sum directly): 从数据库中获取这些ID对应的值,并将它们相加(或执行直接计算总和的查询):

<input type="checkbox" name="checkedRows" value="${idOfCurrentRow}">

In the servlet handling the form submission: 在处理表单提交的servlet中:

String[] checkedIds = request.getParameterValues("checkedRows");

你可以得到它

String[] values = req.getParameterValues("checkbox");

Change this line: 改变这一行:

 out.println("<td><input type=\\"checkbox\\" name=\\"checkbox\\"></td>"); 

To read: 读书:

out.println("<td><input type=\"checkbox\" name=\"selected\" value=\""+ex.getExpenses().get(i-1)+"\"></td>");

This will create checkboxes that you can identify themselves by the value-tag. 这将创建复选框,您可以通过value-tag识别自己。 I hope that the "number" field is a primary key or the like. 我希望“数字”字段是主键等。

Then on next run. 然后在下一次运行。 You can get all checked values by: 您可以按以下方式获取所有选中的值

String[] selected = request.getParameters("selected");

now, you can just add up the numbers. 现在,你可以加上数字。 I don't quite understand how you Expenses-list looks like from the code. 我不太明白你的费用如何从代码中看起来像。 Here is just an illustration that will not work but could give you the idea on how to do it: 这里只是一个图示不起作用,但可以给你如何做的想法:

int sum = 0;
for (int i = 0; i < ex.getExpenses().size(); i++)
  for(int selectedIndex = 0; selectedIndex < selected.length; ++selectedIndex)
    if (ex.getExpenses().get(i) == Integer.parseInt(selected[selectedIndex]))
      sum += ex.getExpenses().get(i-1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM