将数据从 javascript 传递到 servlet
我有复选框的值,一旦单击删除这些值,并且类型必须发送到名为 DeleteServlet 的 servlet。请帮助我如何发送这些值?
<html>
<head>
<script type="text/javascript">
function get_check_value(chkbox){
if(chkbox.checked){
var Values=chkbox.value;
//send this Values to servlet
}
}
function doDelete(theClick){
var type=theClick;
//send this type to servlet
alert(type);
}
</script>
</head>
<%String user=request.getParameter("Users"); %>
<body>
<%
String User[]=user.trim().split(" ");
for(int i=0;i<User.length;i++){%>
<center>
<form name='list' action='AddUserServlet'>
<% out.println("<input type='checkbox' name='user" + i + "' value='"+ User[i] + "' onClick='return get_check_value(this)'>" + User[i]+ "</input>" ); %>
</form>
<%}%>
<input type="submit" value="ADD" name="b1" >
<input type="submit" value="MODIFY" name="b1">
<input type="submit" value="DELETE" name="b1" onClick="return doDelete(value)" >
</center>
</body>
</html>
我在警报框中尝试了这个“document.list.user.value”,它作为未定义的对象出现。为了发送到 servlet,我尝试了以下行,但没有读取任何内容
chk_val=chkbox.value;
document.add.action = "AddUserServlet?type=delete&name="+chk_val;
document.add.submit();
I have values of checkbox and as soon as I click delete the values and the type must be sent to a servlet called DeleteServlet. Please help me how to send the values?
<html>
<head>
<script type="text/javascript">
function get_check_value(chkbox){
if(chkbox.checked){
var Values=chkbox.value;
//send this Values to servlet
}
}
function doDelete(theClick){
var type=theClick;
//send this type to servlet
alert(type);
}
</script>
</head>
<%String user=request.getParameter("Users"); %>
<body>
<%
String User[]=user.trim().split(" ");
for(int i=0;i<User.length;i++){%>
<center>
<form name='list' action='AddUserServlet'>
<% out.println("<input type='checkbox' name='user" + i + "' value='"+ User[i] + "' onClick='return get_check_value(this)'>" + User[i]+ "</input>" ); %>
</form>
<%}%>
<input type="submit" value="ADD" name="b1" >
<input type="submit" value="MODIFY" name="b1">
<input type="submit" value="DELETE" name="b1" onClick="return doDelete(value)" >
</center>
</body>
</html>
I tried this "document.list.user.value" in an alert box it comes as undefined object. To send to a servlet I tried the following line but nothing is read
chk_val=chkbox.value;
document.add.action = "AddUserServlet?type=delete&name="+chk_val;
document.add.submit();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你根本不需要Javascript。
只需更改您的代码,即可生成 HTML 表单,如下所示:(
复选框值必须是唯一的用户 ID)
然后在您的
AddUserServlet
中使用request.getParameterValues()
:users
数组将包含检查的值(1、2、3、4 和/或 5)。You don't need Javascript for this at all.
Just change your code so that the HTML form is generated as follows:
(the checkbox value must be the unique user ID)
Then use
request.getParameterValues()
in yourAddUserServlet
:The
users
array will contain the checked values (1, 2, 3, 4 and/or 5).你可以使用 jQuery javascript 库吗?它很容易使用,你会发现很多教程。
这是关于 get 方法(AJAX)的很好的教程 http://api.jquery.com/jQuery.get/
不使用 javascript 是否可以做到这一点?
Could you use jQuery javascript libary. It is easy to use and you will find lots of tutorials.
Here is good tutorial about get method(AJAX) http://api.jquery.com/jQuery.get/
Is it possible to do it without javascript?