我目前正在学习JSP。我有一份水果清单,每个水果旁边都有复选框。我想在用户选择一个或多个水果并提交后显示消息或链接。我怎么能实现它?这是我的代码。通过我的代码不起作用的方式......
<%@ page language="java" contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Insert title here</title>
<title>JSP Multiple Checkbox</title>
</head>
<body>
<form name="form1" action="">
<h3>Select your favorite Fruits</h3>
<p><input type="checkbox" name="fruit" value="Mango"/>Mango</p>
<p><input type="checkbox" name="fruit" value="Apple"/>Apple</p>
<p><input type="checkbox" name="fruit" value="Grapes"/>Grapes</p>
<p><input type="checkbox" name="fruit" value="Papaya"/>Papaya</p>
<p><input type="checkbox" name="fruit" value="Lychee"/>Lychee</p>
<p><input type="checkbox" name="fruit" value="Pineapple"/>Pineapple</p>
<p><input type="submit" value="submit"/>
</form>
<%
String select[] = request.getParameterValues("id");
if (select != null && select.length != 0) {
out.println("You have selected: ");
for (int i = 0; i < select.length; i++) {
out.println(select[i]);
}
//show message or a link instead of printing
}
%>
</body>
</html>
答案 0 :(得分:2)
不要忘记JSP是在服务器端执行的。您要做的是在用户交互时在客户端动态显示内容。这不能用taglib或直接在JSP中完成,因为你的JSP无法知道用户在做什么。 JSP只允许您生成将发送到客户端的HTML。之后,JSP不会干扰。 有两种方法可以做到这一点:
onChange
事件,并在此事件中对界面执行某些操作; 答案 1 :(得分:0)
您需要更改&#39;操作&#39;表单的参数到另一个jsp文件并移动行
<%
String select[] = request.getParameterValues("fruit");
if (select != null && select.length != 0) {
out.println("You have selected: ");
for (int i = 0; i < select.length; i++) {
out.println(select[i]);
}
//show message or a link instead of printing
}
%>
到新的jsp。 要显示消息或链接,您需要使用javascript。 PS:行request.getParameterValues(&#34; id&#34;) 是错的。给 request.getParameterValues(&#34;水果&#34)