获取复选框的选中值并将其发送到 servlet

发布于 2024-10-08 05:31:41 字数 2552 浏览 3 评论 0原文

我必须显示前面带有复选框的用户列表。我现在想选择一些名称并将其从数据库中删除。我已经编写了代码来显示用户。我不知道如何获取所选值并将其删除。请帮助我,这是我尝试过的

<%@ 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>
<%String user=request.getParameter("Users"); %>
<script type="text/javascript">
var chk="dummy";
 function doSubmit(theClick){
        window.open("AddUser.jsp");
        return false;
        }
function setValue(chVal)
{
    chk=chk+","+chVal;
    alert(chk);

}       
function doDelete(theClick){
    document.buttons.action = "AddUserServlet?type=delete&name="+chk;
    alert(chk);
    document.buttons.submit();          

}
</script>
</head>
<body>
<center>

<!--  action="AddUserServlet"--> 

<%
try{
String User[]=user.trim().split(" ");
for(int i=0;i<User.length;i++){
    out.println("<html>");
    out.println("<body>");
    out.println("<form>");
    out.println("<input type='checkbox' value='User[i]' onClick='setValue(this.value)' name='user'>"+User[i]+"</input>");
    //out.println("<input type='checkbox' value='User'+i+ name='user1'>"+User[i]+"</br>"+"</form>");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
    //out.println("<input type='checkbox' value='User'+i+ name='user1'>"+User[i]+"</br>"+"</form>");
}
}catch(Exception e){
    e.printStackTrace();
}

%>

<!--
<form method="Post" name="buttons"> 
 <input type='checkbox' value='User' onClick="setValue(this.value)" name='user1'></input>
<input type='checkbox' value='User1' onClick="setValue(this.value)" name='user1'></input>
<input type='checkbox' value='User2' onClick="setValue(this.value)" name='user1'></input>
<div id="add"></div>
</form>-->

<form method="Post" name="add">
<input type="submit" value="ADD" onclick="return doSubmit(this)">
<input type="submit" value="MODIFY" onClick="return doSubmit(this)">
<input type="submit" value="DELETE" onClick="return doDelete(this)">
</form>
</center>
</body>
</html>

。上面的代码显示名称列表以及复选框。但返回值“user”而不是数据库中的名称。另外,如果我取消选中一个名称,它会再次返回用户。

[*]abc-----> returns dummy,user
if i uncheck this []user returns dummy,user0,user1 

我需要获取用户名列表。

I have to display a list of users with checkbox in its front. I now want to select some of the names and delete it in database. I have written code to display users. I don't know how to get the selected values and delete it. Please help me here is what I have tried

<%@ 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>
<%String user=request.getParameter("Users"); %>
<script type="text/javascript">
var chk="dummy";
 function doSubmit(theClick){
        window.open("AddUser.jsp");
        return false;
        }
function setValue(chVal)
{
    chk=chk+","+chVal;
    alert(chk);

}       
function doDelete(theClick){
    document.buttons.action = "AddUserServlet?type=delete&name="+chk;
    alert(chk);
    document.buttons.submit();          

}
</script>
</head>
<body>
<center>

<!--  action="AddUserServlet"--> 

<%
try{
String User[]=user.trim().split(" ");
for(int i=0;i<User.length;i++){
    out.println("<html>");
    out.println("<body>");
    out.println("<form>");
    out.println("<input type='checkbox' value='User[i]' onClick='setValue(this.value)' name='user'>"+User[i]+"</input>");
    //out.println("<input type='checkbox' value='User'+i+ name='user1'>"+User[i]+"</br>"+"</form>");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
    //out.println("<input type='checkbox' value='User'+i+ name='user1'>"+User[i]+"</br>"+"</form>");
}
}catch(Exception e){
    e.printStackTrace();
}

%>

<!--
<form method="Post" name="buttons"> 
 <input type='checkbox' value='User' onClick="setValue(this.value)" name='user1'></input>
<input type='checkbox' value='User1' onClick="setValue(this.value)" name='user1'></input>
<input type='checkbox' value='User2' onClick="setValue(this.value)" name='user1'></input>
<div id="add"></div>
</form>-->

<form method="Post" name="add">
<input type="submit" value="ADD" onclick="return doSubmit(this)">
<input type="submit" value="MODIFY" onClick="return doSubmit(this)">
<input type="submit" value="DELETE" onClick="return doDelete(this)">
</form>
</center>
</body>
</html>

The code above displays list of names along with checkboxes. But returns value "user" and not the name in database. Also if I uncheck a name it again returns user.

[*]abc-----> returns dummy,user
if i uncheck this []user returns dummy,user0,user1 

I need get the username list.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看春风乍起 2024-10-15 05:31:41

您将在每个复选框周围打印一个全新的

。您的 HTML 最终在浏览器中显示如下:

<html>
    <head></head>
    <body>
        <html><body><form><input type="checkbox"></form></body></html>
        <html><body><form><input type="checkbox"></form></body></html>
        <html><body><form><input type="checkbox"></form></body></html>
        <form><input type="submit"></form>
    </body>
</html>

This is 语法无效 HTML。您需要重写代码,以便所有复选框和提交按钮都以相同形式结束:

<html>
    <head></head>
    <body>
        <form>
            <input type="checkbox">
            <input type="checkbox">
            <input type="checkbox">
            <input type="submit">
        </form>
    </body>
</html>

然后您也不需要那些丑陋的 JavaScript 解决方法。您只需为复选框指定相同的名称,但指定不同的值即可。这样您就可以通过 HttpServletRequest#getParameterValues() 获取检查的值。

String[] users = request.getParameterValues("user");

You're printing a whole new <html> and <form> around every single checkbox. Your HTML ends up in browser like as:

<html>
    <head></head>
    <body>
        <html><body><form><input type="checkbox"></form></body></html>
        <html><body><form><input type="checkbox"></form></body></html>
        <html><body><form><input type="checkbox"></form></body></html>
        <form><input type="submit"></form>
    </body>
</html>

This is syntactically invalid HTML. You need to rewrite your code so that all checkboxes and the submit button ends up in the same form:

<html>
    <head></head>
    <body>
        <form>
            <input type="checkbox">
            <input type="checkbox">
            <input type="checkbox">
            <input type="submit">
        </form>
    </body>
</html>

Then you also don't need those ugly JavaScript workarounds. You just give the checkboxes the same name, but a different value. This way you can just grab the checked values by HttpServletRequest#getParameterValues().

String[] users = request.getParameterValues("user");
云仙小弟 2024-10-15 05:31:41

您需要为每个用户的每个复选框分配不同的名称。这样,获取 POST 表单的 servlet 可以检查每个复选框名称(例如 user1...userX),如果设置了其值,则可以删除相应的用户。请注意,如果未设置该复选框,则不会为该名称发送任何表单编码参数,因此您实际上可以仅检查发送了哪些名称并删除它们。

You'll need to assign different names to each checkbox per user. This way, the servlet that gets the POSTed form can check each checkbox name (e.g. user1...userX) and if its value is set then you can delete the corresponding user. Note that if the checkbox is not set then there will be no form encoded parameter sent for that name, so you could really just check to see which names were sent and delete those.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文