2015-04-02 49 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

相關問題