How to pass data from selected rows using checkboxes from JSP to the server

后端 未结 3 1917
抹茶落季
抹茶落季 2021-01-07 07:06

I\'d like to know if there\'s any way to send data to the server for the selected rows using the checkboxes I\'ve put on those rows? I mean , how can I send only the data (i

相关标签:
3条回答
  • 2021-01-07 07:47

    According to HTML's FORM spec, items with same name will be grouped to single value separated by comma. So just bind your bean to form result and split value of 'selectedItems' by comma.

    0 讨论(0)
  • 2021-01-07 07:53

    First off, I'd suggest to make sure that your server-side script ignores the "non-checked" data, because you never can be sure that the JavaScript will actually work or isn't manipulated by the user.

    However currently you have no way too associate each check box with its mileage input, so you should consider giving each an individual name such as {numberplate}-mileage.

    That would also simplify the JavaScript. (I'm using jQuery here, becuase it quicker):

    jQuery("input[name=selectedItems]").click(
       var chkbox = $(this);
       $("input[name=" + checkbox.val() + "-mileage]").attr("disabled", checkbox[0].checked ? "" : "disabled");
    );
    

    (Disabled input elements don't submit their value).

    0 讨论(0)
  • 2021-01-07 07:58

    Change the mileage input as follows:

    <c:forEach items="${list}" var="item">  
        <tr>  
            <td align="center">  
                <input type="checkbox" name="selectedItems"   
                    value="<c:out value="${item.numberPlate}"/>"/>  
            </td>
            <td align="left"><c:out value="${item.numberPlate}"/></td>  
            <td align="left"><c:out value="${item.driver.fullName}" /></td>  
            <td align="left"><input type="text" name="mileage_<c:out value="${item.numberPlate}"/>" value="" /></td>  
        </tr>  
    </c:forEach>
    

    Gather it as follows:

    String[] selectedItems = request.getParameterValues("selectedItems");
    for (String selectedItem : selectedItems) {
        String mileage = request.getParameter("mileage_" + selectedItem);
        // ...
    }
    

    No need for nasty JS workarounds.

    0 讨论(0)
提交回复
热议问题