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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29410913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:13:21  来源:igfitidea点击:

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

javajspcheckbox

提问by Dola Chakraborty

I am a beginner in jsp.I want to get data from database and show checked on a checkbox depending on db value i.e. 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. here is my jsp:

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

采纳答案by Masudul

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

回答by Michael Lowden

you can simplify the middle bit in the other posters suggestion with a basic IF test. Because it's only going to be 1 or 0 (choose is overkill 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}". The JSTL is smart enough to handle all angles (including NULL). 1=true, 0=false, NULL=false.

您可以通过基本的 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>

For your second bit about the 'unchecked' box passing invalid values. That's a super annoying thing with 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). Different "ID", but same "NAME" so the Servlet will see it and capture the 'unchecked' value from it instead of the NULLed original checkbox:

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

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