简体   繁体   English

如何将复选框值(选中/未选中)作为jsp中的href参数传递给控制器

[英]how to pass checkbox value(checked/unchecked) to controller as href parameter in jsp

I am a beginner in jsp.I want to get data from database and show checked on a checkbox depending on db value ie 0 or 1.This is working for me.But I also want o that when I check or uncheck the checkbox it will reset value and pass it to controller as a href parameter. 我是jsp的初学者。我想从数据库中获取数据并根据db值显示复选框处于选中状态,即0或1.这对我有用。但是我也希望当我选中或取消选中该复选框时,它将重置值并将其作为href参数传递给控制器​​。 here is my jsp: 这是我的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>

To update checkbox value, the easiest way is to use form in loop. 要更新复选框的值,最简单的方法是循环使用表单。 Try 尝试

<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>

Edit: 编辑:

You need a Servlet to get updated value 您需要一个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.
 }

you can simplify the middle bit in the other posters suggestion with a basic IF test. 您可以使用基本的IF测试来简化其他海报建议中的中间部分。 Because it's only going to be 1 or 0 (choose is overkill IMO). 因为它只会是1或0(所以选择IMO是过大的选择)。 Technically because it's 1/0 you don't even need to ==1 comparitor, just reference it raw in the test="" block as test="${investorAcc.sms_sub}". 从技术上讲,因为它是1/0,所以您甚至不需要== 1比较器,只需在test =“”块中将其原始引用为test =“ $ {investorAcc.sms_sub}”。 The JSTL is smart enough to handle all angles (including NULL). JSTL足够聪明,可以处理所有角度(包括NULL)。 1=true, 0=false, NULL=false. 1 = true,0 = false,NULL = false。

<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>

For your second bit about the 'unchecked' box passing invalid values. 关于“未选中”框传递无效值的第二点。 That's a super annoying thing with HTML. HTML真是太烦人了。 Unchecked boxes get 'dropped to null' when a form is submitted. 提交表单时,未选中的框会被“丢弃为空”。 A lot of people are supplementing with a 2nd hidden input to get this done without NULL errors (note, it MUST be second). 许多人都在补充第二个隐藏输入,以完成此操作而没有NULL错误(请注意,它必须是第二个)。 Different "ID", but same "NAME" so the Servlet will see it and capture the 'unchecked' value from it instead of the NULLed original checkbox: 不同的“ ID”,但相同的“ NAME”,因此Servlet将看到它并捕获其中的“未检查”值,而不是NULLed原始复选框:

ex: 例如:

<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 -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM