如何从复选框获取值并将其传递到另一个JSP页面? [英] how to get values from checkbox and pass it to the another jsp page?
本文介绍了如何从复选框获取值并将其传递到另一个JSP页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用for
循环创建了许多复选框.现在,我想从复选框中获取是否被打勾的值.
I have created many checkboxes using a for
loop. Now I want to get the values from checkboxes if they are ticked or not.
当一个复选框被打勾,那么它的标签必须被传递到另一个JSP页面.但是我无法正确实现它.
When a checkbox is ticked, then its label must be passed to another JSP page. But I am unable to achieve it properly.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1><center>REGISTER FORM</center></h1>
<%
String[] stArray=new String[40];
ArrayList ar = new ArrayList();
int idcounter = 0;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/registerdb", "root", "");
PreparedStatement ps = con.prepareStatement("select leave_types from leaves");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
String array_value = rs.getString("leave_types");
ar.add(array_value);
}
// out.println(ar);
request.setAttribute("LEV_ARRAY", ar);
}
catch(Exception e)
{
out.println(e);
}
%>
<form action = "insertdata.jsp" name="myform" method="post">
<%
for(int i = 0; i<ar.size(); i++)
{
out.println(ar.get(i));
%>
<input id ="<%=idcounter%>" type="checkbox" name = "" value="" />
<%
idcounter++;
}
// String[] selectedCheckboxes = request.getParameterValues("selected");
%>
<center><button type= "submit" name="action">SIGN UP</button></center>
</form>
</body>
</html>
推荐答案
您可以按照以下步骤操作:
You can do like below :
通过ar.get(i)
作为复选框中的值:
Pass ar.get(i)
as values in checkboxes :
<form action = "insertdata.jsp" name="myform" method="post">
<%
for(int i = 0; i<ar.size(); i++)
{
out.println(ar.get(i));
%>
<!--name=abc will be used in jsp to get value selected in checkboxes-->
<input id ="<%=idcounter%>" type="checkbox" name = "abc" value="<%=ar.get(i)%>" />
<%
idcounter++;
}
%>
<center><input type= "submit" name="action" value="SIGN UP"/></center>
</form>
然后,要在jsp page
中达到values
以上,请执行以下操作:
Then ,to get above values
in jsp page
do like below:
<!--getting values selected using "abc"-->
<%String check[]= request.getParameterValues("abc");
<!--checking for null values-->
if(check!= null){%>
<!--there might be more one checkbox selected so, using loop-->
<%for(int i=0; i<check.length; i++){%>
<!--printing values selected-->
<%=check[i]%>
<%}
}%>
这篇关于如何从复选框获取值并将其传递到另一个JSP页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文