How to get checked checkboxes in JSP

前端 未结 2 1867
故里飘歌
故里飘歌 2020-12-07 01:07

How can I get/set checkbox value using jstl and delete only those record from the database where the checkbox is checked? can you also advise how to use ternary operators in

相关标签:
2条回答
  • 2020-12-07 01:44

    Your checkbox has currently no value associated with the parameter name at all:

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

    So it's hard to find out the checked ones. You need to give the checkbox a value which uniquely identifies the selected item. In your particular example, the student ID seems to be an obvious choice:

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

    (by the way, why are you duplicating the entity name in the property name? why not just name it id so that you can just self-documentary use ${student.id}? also your var="students" is kind of odd, it is referring only one student, so just name it var="student"; the ${studentList} can better be named ${students})

    When the form is submitted, all checked value are available as follows:

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

    Finally, just pass it through to your DAO/service class which does the business job:

    studentService.delete(selectedStudentIds);
    

    See also:

    • How to transfer data from JSP to servlet when submitting HTML form
    • ServletRequest.getParameterMap() returns Map<String, String[]> and ServletRequest.getParameter() returns String?
    • Send an Array with an HTTP Get
    0 讨论(0)
  • 2020-12-07 01:57

    this may help you.

    In ajax call:

    var boolValue= $(this).closest(".tr").find('.checkboxClass').is(':checked');
    
    $.post("/api/dosomething", {                                     
        someSettings : boolValue
    })
    
    0 讨论(0)
提交回复
热议问题