Java 获取复选框值
我向servlet发送一个请求,它从db中返回一些数据,并在表和复选框中使用结果数据,servlet本身使用out.println,现在我需要使用复选框选择数据进行进一步操作,现在我不知道如何获取所选文本框的值 这是我的servlet代码Java 获取复选框值,java,html,jsp,servlets,Java,Html,Jsp,Servlets,我向servlet发送一个请求,它从db中返回一些数据,并在表和复选框中使用结果数据,servlet本身使用out.println,现在我需要使用复选框选择数据进行进一步操作,现在我不知道如何获取所选文本框的值 这是我的servlet代码 ps=connection.prepareStatement("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.s
ps=connection.prepareStatement("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.scenario_id;");
ResultSet rs=ps.executeQuery();
out.println("<table>");
/*out.println(executionValues.append("<tr><td>").append("Test Case Name :").append("</td><td>").append("Scenario Name :").append("</td></tr>"));*/
while(rs.next()){
out.println("<li class='panel' value='"+rs.getInt("scenario_id")+"'><b>Scenario Name:</b>"+rs.getString("scenario_name")+"</li><b>Test Case Name:</b>"+rs.getString("tc_name")+"<input type=\"checkbox\" name=\"checkbox\"></li>");
}
你应该把它移走
您的sql查询:
("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.scenario_id;");
你应该像这样改变:
("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.scenario_id");
您正在打印一个全新的复选框。您的HTML在浏览器中显示为:
<html>
<head></head>
<body>
<html><body><form><input type="checkbox"></form></body></html>
<form><input type="submit"></form>
</body>
</html>
那么你也不需要那些丑陋的JavaScript变通方法了。您只需为复选框指定相同的名称,但不同的值。这样,您就可以通过HttpServletRequestgetParameterValues获取选中的值
例如:
checkboxValues获取所有以逗号分隔的值
请参阅此链接:
亲爱的suganth,现在将html代码放在servlet中是不合适的。而是使用jsp页面。 但如果你必须这样做,那么你可以提供
if(conditionMatch){
// code for checked box
}
else{
// code for unchecked box
}
Hope this helps
谢谢jmail,如果你知道如何获取选中的复选框值,请告诉我,看看示例和链接
String[] users = request.getParameterValues("user");
<form name="input" action="html_form_action" method="get">
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car
<br><br>
<input type="submit" value="Submit">
</form>
String checkboxValues = request.getParameter("vehicle");
if(conditionMatch){
// code for checked box
}
else{
// code for unchecked box
}
Hope this helps