[英]How to pass parameters to a Servlet from JSP using javascript
下面是我編寫的用於創建圓形按鈕數組的代碼,這些按鈕被動態賦予一個值。 這些按鈕在單擊時被刪除,並且按鈕的相應值被傳遞到java腳本中的數組Object。 現在,我的要求是將這些值(已刪除的按鈕值)作為一個整體發送給Servlet,即當我完成任務時(可能是在刪除一個按鈕或兩個按鈕之后)。
為了實現這一點,我在div之后創建了另一個按鈕,並以名稱= DeletedItems的形式包含了整個正文。 但是只需單擊任何按鈕,代碼就會因為表單而刷新。 誰能建議我該如何進行? 注意:由於該過程無法正常進行,因此我未在下面的代碼中包含div部分和form部分。
碼
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="css/main.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
var arr=new Array();
function hButton(id)
{
document.getElementById(id).setAttribute("style","background:white");
//document.getElementById(id).disabled=true;
//arr[arr.length+1]=id;
arr.push(id);
document.getElementById("id-show").textContent=arr+"</br>";
}
function showId(id)
{
document.getElementById(id).setAttribute("title",id);
}
</script>
</head>
<body>
<div id="big_wrapper">
<table>
<%for(int j=1;j<=3;j++){ %>
<tr>
<%for(int i=1;i<=5;i++){ %>
<td> <button class="btn" style="background:red" id="<%=j%>,<%=i%>" onmouseOver='showId(this.id)' onclick='hButton(this.id)'> </button> </td>
<%} %>
</tr>
<%} %>
</table>
</div>
<div id="id-show">
</div> </body> </html>
Main.css文件
.btn{
display:block;
width:50px;
height:50px;
line-height:50px;
border: 2px solid #f5f5f5;
border-radius: 50%;
text-align:center;
text-decoration:none;
font-size:20px;
font-weight:bold;
}
.btn:hover {
opacity:0.9;
color:#FFFFFF;
font-size:20px;
font-weight:700;
background: #262626;
}
#big_wrapper{
border:1px solid black;
width:1000px;
margin:20px auto;
text-align:left;
}
.theText{
opacity:0;
}
.btn:hover .theText
{
opacity:0.9;
color:#FFFFFF;
font-size:20px;
font-weight:700;
background: #262626;
}
#some-element {
border: 1px solid #ccc;
display: none;
font-size: 10px;
margin-top: 10px;
padding: 5px;
text-transform: uppercase;
}
#some-div:hover #some-element {
display: block;
}
執行以下操作:
在JSP中:
<script>
function submit() {
var someVariable = "value to pass to server";
document.getElementById("someFieldId").value = someVariable;
document.form[0].submit();
}
</script>
<form name="someForm" method="POST">
<input type="hidden" name="someField" id="someFieldId" />
<input type="button" name="Submit" onclick="submit()" />
</form>
在Servlet中:
String variable = request.getParamater("someField");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.