2015-04-02 48 views
1

我是jsp.I的初学者,想从数据库中获取数据并根据db值检查复选框,例如0或1。正在为我工​​作。但我也希望当我检查或取消选中复选框时,它将重置值并将其作为href参数传递给控制器​​。 这里是我的jsp:如何将复选框值(选中/取消选中)传递给控制器​​作为hsp参数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> 
+0

ID必须为你使用'为什么你要chk'为ID多次 – Arvind 2015-04-02 11:02:48

+0

独特将它作为参数传递给href? – vivekpansara 2015-04-02 11:08:03

+0

,因为我在我的控制器中映射了/ updateInvestorAcc,而且我必须不仅传递复选框的值,还要传递list.and中的id。我知道没有其他办法可以做到这一点:-p – 2015-04-02 11:12:24

回答

0

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

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

感谢您的帮助 但是如何才能我的地图控制器的方法为表单提交? – 2015-04-02 11:13:58

+0

@dolachky,看看编辑部分。 – Masudul 2015-04-02 11:27:04

+0

获取ID是好的,但复选框没有返回正确的值,我的意思是如果一个复选框设置为cheked,那么在取消选中它之后,我会得到空值。并且未选中复选框,即由于investorAcc.sms_sub = 0而未选中的复选框,它们也有返回null值。有没有办法设置一个复选框取消检查使用相同的方式,我们设置它检查? – 2015-04-05 07:34:21

相关问题