【问题标题】:how to pass checkbox value(checked/unchecked) to controller as href parameter in jsp如何将复选框值(选中/未选中)作为jsp中的href参数传递给控制器
【发布时间】:2018-10-17 22:12:34
【问题描述】:

我是 jsp 的初学者。我想从数据库中获取数据并根据 db 值(即 0 或 1)在复选框上显示选中状态。这对我有用。但我也希望当我选中或取消选中复选框它将重置值并将其作为 href 参数传递给控制器​​。 这是我的jsp:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Investor Account</title>
</head>

    <body>

  <script type="text/JavaScript">
            function updateCheck(){

                if(document.getElementById('chk').checked){
                     document.getElementById('chk').value = 1;
                     location.href=""
                     alert(document.getElementById('chk').value);



                 }
                else{
                      document.getElementById('chk').value = 0;
                      alert(document.getElementById('chk').value);


                }

            }

</script>
        <div align="center">
            <h1>Investor Account List</h1>

            <table border="1">
                <th>ID</th>
                <th>Investor Code</th>
                <th>Investor Name</th>
                <th>SMS Subscriber</th> 
                <th>Action</th>         
                <c:forEach var="investorAcc" items="${listInvestorAcc}" varStatus="st">
                <tr>
                    <td>${st.index + 1}</td>
                    <td>${investorAcc.investor_code}</td>
                    <td>${investorAcc.investor_name}</td>
                    <td> <input type="checkbox" id="chk" name="chkSms" value=  <c:if test="${investorAcc.sms_sub==1}">1</c:if> onclick="updateCheck();"
                    <c:if test="${investorAcc.sms_sub==1}">checked="checked"</c:if>/>

                  <td>

                      <a href="updateInvestorAcc?id=${investorAcc.id}&chkSms=<c:if test="${chkSms==1}">1</c:if>"> Update </a>
                   </td>

                </tr>

                </c:forEach>             
            </table>
        </div>
    </body>
</html>

【问题讨论】:

  • ID 必须是唯一的,您多次使用 chk 作为 id
  • 为什么要在href中作为参数传递?
  • 因为我的控制器中有 /updateInvestorAcc 的映射,而且我不仅要传递复选框值,还要传递列表中的 id。我不知道其他方法可以做到这一点:-p跨度>

标签: java jsp checkbox


【解决方案1】:

要更新复选框值,最简单的方法是在循环中使用表单。试试

<c:forEach var="investorAcc" items="${listInvestorAcc}" varStatus="st">
   <form action="updateInvestorAcc">
      <input type="hidden" name="id" value="${investorAcc.id}"/>
      <tr>
          <td>${st.index + 1}</td>
          <td>${investorAcc.investor_code}</td>
          <td>${investorAcc.investor_name}</td>
          <td>
              <c:choose>
                 <c:when test="${investorAcc.sms_sub==1}">
                   <input type="checkbox" id="chk" name="chkSms" 
                        value="${investorAcc.sms_sub}" checked="checked"/>
                 </c:when>
                 <c:otherwise>
                     <input type="checkbox" id="chk" name="chkSms" 
                           value="${investorAcc.sms_sub}"/>
                 </c:otherwise>
              </c:choose><td> <!-- end of checkbox -->
          <td><input type="submit" value="Update"></td>
      </tr>
   </form> 
</c:forEach>

编辑:

你需要一个 Servlet 来获取更新的值

@WebServlet("/updateInvestorAcc")
public class UpdateInvestorAcc extends HttpServlet {

 public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {

     String idStr=request.getParameter("id");
     int id= Integer.parseInt(idStr);// If you need to parse to int
     String[] chkSms=request.getParameterValues("chkSms");
     boolean isChkSms =false;
     if(chkSms !=null && chkSms.length > 0){//If checkbox is checked than assign it with true or 1       
         isChkSms=true;  
     }

    // Now update the DB with id and checkbox value.
 }

【讨论】:

  • 感谢您的帮助,但如何映射控制器方法以提交表单?
  • 获取 id 是可以的,但是复选框没有返回正确的值,我的意思是如果一个复选框被设置为 cheked,那么在取消选中它之后我得到空值。和未选中的复选框,即未选中的复选框是因为InvestorAcc.sms_sub=0,他们也返回空值。有没有办法像我们设置它检查一样设置一个取消选中的复选框?
【解决方案2】:

您可以通过基本的 IF 测试来简化其他海报建议中的中间部分。因为它只会是 1 或 0(选择是过度杀伤 IMO)。从技术上讲,因为它是 1/0,您甚至不需要 ==1 比较器,只需在 test="" 块中将其原始引用为 test="${investorAcc.sms_sub}"。 JSTL 足够聪明,可以处理所有角度(包括 NULL)。 1=真,0=假,NULL=假。

<c:forEach var="investorAcc" items="${listInvestorAcc}" varStatus="st">
<form action="updateInvestorAcc">
    <input type="hidden" name="id" value="${investorAcc.id}"/>
    <tr>
        <td>${st.index + 1}</td>
        <td>${investorAcc.investor_code}</td>
        <td>${investorAcc.investor_name}</td>
        <td>
            <input  type="checkbox" id="chk" name="chkSms" 
                    value="${investorAcc.sms_sub}" <c:if test="${investorAcc.sms_sub==1}">checked="checked"</c:if>/>
        <td> <!-- end of checkbox -->
        <td><input type="submit" value="Update"></td>
    </tr>
</form> 
</c:forEach>

关于传递无效值的“未选中”框的第二点。这对于 HTML 来说是一件非常烦人的事情。提交表单时,未选中的框会被“删除为空”。很多人都在补充第二个隐藏输入来完成此操作而不会出现 NULL 错误(注意,它必须是第二个)。不同的“ID”,但相同的“NAME”,因此 Servlet 会看到它并从中捕获“未选中”值,而不是 NULLed 原始复选框:

例如:

<td>
    <input  type="checkbox" id="chk" name="chkSms" 
            value="${investorAcc.sms_sub}" <c:if test="${investorAcc.sms_sub==1}">checked="checked"</c:if>/>
    <input  type="hidden" id="chk_0" name="chkSms"
            value="0"/>
<td> <!-- end of checkbox -->

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 2014-12-29
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2015-07-07
    相关资源
    最近更新 更多