我在 index.jsp
中,我想在单击 href
标记后传递一些数据。该页面的代码如下:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="js/jquery-3.2.1.min.js"></script>
<style>
a{
padding: 10px !important;width:200px;
text-decoration:none !important;
background-color: #0B70BE;
background-image: -moz-linear-gradient(center center , rgb(11, 112, 190) 0%, rgb(11, 112, 190) 100%) !important;
border-radius: 2px !important;
border: 2px solid rgb(43, 125, 185) !important;
font-weight: bold;
font-size: 13px !important;
font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
height: 26px !important;
text-shadow: none !important;
color: rgb(255, 255, 255) !important;
}
</style>
<script type="text/javascript">
$('#myForm1').click(function (e)
{
e.preventDefault(); // prevent the link from actually redirecting
$('#myForm1').submit();
});
</script>
</head>
<body>
<div>TODO write content</div>
<%
String name = "1231";
int rollNo = 121;
%>
<form method="get" id="myForm1" action="hello.jsp">
<Input type="Hidden" name="name" id="name" value="<%=name%>">
<Input type="Hidden" name="rollNo" id="rollNo" value="<%=rollNo%>">
<a href="hello.jsp">Please Enroll Finger 1</a>
</form>
</body>
</html>
点击href
标签后,我将转到hello.jsp
。在该页面中,我想检索 name
和 rollNo
值。所以我有 hello.jsp
页面的代码:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
String name = request.getParameter("name");
String rollNo = request.getParameter("rollNo");
out.println("Student name "+name+"<br>");
out.println("Student Roll No is "+rollNo);
%>
</body>
</html>
点击第一页中的href
标签后,我进入hello.jsp
页面。但在第二页中,我得到的 name
和 rollNo
的值为 null。我得到以下输出:
Hello World!
Student name null
Student Roll No is null
我已接受帮助from this link . 我无法理解为什么我在第二页中得到空值。我该如何解决这个错误?请帮我。
更新信息
我检查了index.jsp
的代码。代码如下:
最佳答案
隐藏值位于表单内,因此您需要一个表单提交来将值提交到下一页。如果直接将页面放入 href 属性中,浏览器将转到该页面,而不进行任何表单提交。
因此,您需要将 javascript 代码放在 href 中来触发表单提交操作,如下所示。
<form method="get" id="myForm1" action="hello.jsp">
<Input type="Hidden" name="name" id="name" value="<%=name%>">
<Input type="Hidden" name="rollNo" id="rollNo" value="<%=rollNo%>">
<a href="$('#myForm1').submit();">Please Enroll Finger 1</a>
</form>
关于javascript - jsp中如何通过点击href标签传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46538529/