【问题标题】:How to get checked checkboxes in JSP如何在 JSP 中获得选中的复选框
【发布时间】:2012-09-05 23:37:11
【问题描述】:

如何使用 jstl 获取/设置复选框值并仅从选中复选框的数据库中删除那些记录?您能否建议在这种情况下如何在 jstl 中使用三元运算符?

SearchStudent.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Lookup Students</title>
    </head>

    <form method="post" action="deleteStudentServlet" class="form">     
        <body class="body">

        <!-- List results -->

        <c:if test="${not empty studentList}">
            <table border="1" cellspacing="0" cellpadding="0" :>
                <tr>
                    <th></th>
                    <th>ID</th>
                    <th>Title</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th></th>
                </tr>
                <c:forEach var="students" items="${studentList}">
                    <tr>
                        <td><input type="checkbox" name="chkBox"> </td>
                        <td>${students.studentID}</td>
                        <td>${students.title}</td>
                        <td>${students.firstName}</td>
                        <td>${students.lastName}</td>
                        <td><c:url value="UDS" var="url">
                                <c:param name="StudentID" value="${students.studentID}" />
                            </c:url> <a href="${url}">Edit</a></td>
                    </tr>
                </c:forEach>
            </table>
        </c:if>

        <td><input type="submit" name="submit" value="Delete" ></td>
    </form>

        <p>There are ${fn:length(studentList)} results.</p>
    </body>
    </html>

谢谢。

【问题讨论】:

    标签: jsp checkbox jstl


    【解决方案1】:

    您的复选框目前根本没有与参数名称关联的值:

    <input type="checkbox" name="chkBox">
    

    所以很难找出选中的那些。您需要为复选框提供一个唯一标识所选项目的值。在您的特定示例中,学生 ID 似乎是一个明显的选择:

    <input type="checkbox" name="selected" value="${student.studentID}"> 
    

    (顺便说一句,你为什么在属性名中复制实体名?为什么不直接命名为id,这样你就可以自己记录使用${student.id}?你的var="students"也是有点奇怪,它只指一个学生,所以就将其命名为var="student"${studentList} 最好命名为${students}

    提交表单时,所有选中的值如下:

    String[] selectedStudentIds = request.getParameterValues("selected");
    

    最后,只需将其传递给执行业务工作的 DAO/服务类:

    studentService.delete(selectedStudentIds);
    

    另见:

    【讨论】:

    • 我的另一个问题是它怎么知道使用 String[] selectedStudentIds = request.getParameterValues("selected"); 只获取“检查”值
    • 如果我想选择“未选中”的值怎么办?
    • 您熟悉基本的 HTTP 和 HTML 吗?所有 HTML 输入字段都将其值作为 HTTP 请求参数发送。如果您想获得未选中的值,只需从所有可用值(studentList)中减去选中的值(selectedStudentIds)。
    • 是的,我是新手,向像您这样的专家学习。关于你的回答,是不是表示没有选中复选框时,里面的值为null?
    • 请你提供一个减法的例子吗?
    【解决方案2】:

    这可能会对你有所帮助。

    在ajax调用中:

    var boolValue= $(this).closest(".tr").find('.checkboxClass').is(':checked');
    
    $.post("/api/dosomething", {                                     
        someSettings : boolValue
    })
    

    【讨论】:

      猜你喜欢
      • 2010-12-14
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多