2016-06-05 3 views
0

자바 서블릿 애플리케이션을 작성 중입니다. 거기에 내가 선택한 옵션 버튼 수를 얻고 싶은 동적 테이블이 있습니다.확인란을 선택하려면 어떻게합니까?

enter image description here

나는 값이 제출 얼마나 많은 체크 상자를 찾고 싶어요. 그런 다음 제출 된 확인란 값의 수에 따라 루프를 삽입하는 데이터베이스를 만들 수 있습니다.

이 지금까지 내 테이블 인덱스 값을 나타내는 내 id 값으로이 값을 보내 피곤이 있지만 내 양식 코드

<form role="form" id="user_allowed_unis" name="user_allowed_unis" class="form-horizontal" action="../UnisPerUserCtrl" method="post"> 
    <input type="hidden" name="action" id="action" value="USER_ALLOWED_UNIS"> 
    <div class="form-group col-lg-12"> 
     <label class="col-lg-6 control-label">User Email</label> 
       <div class="col-lg-4"> 
        <input type="text" id="email" data-toggle="email" class="form-control" name="email" value="" maxlength="100"> 
      </div> 
    <span class="input-group-btn"><button class="btn btn-default" onclick="createLovWindow('Users');" type="button"><i class="fa fa-search"></i></button></span> 
    </div> 
<div class="input-group"> <span class="input-group-addon">Search</span> 

<input id="filter" type="text" class="form-control" placeholder="Search University..."> 
</div> 
<table class="table table-bordered table-hover table-striped"> 
    <thead> 
     <tr> 
      <th>Id</th> 
      <th>University Id</th> 
      <th>University</th> 
      <th>Selected</th> 
     </tr> 
    </thead> 
    <tbody class="searchable"> 
     <% 
     UniversityMgt uni=new UniversityMgt(); 
     ResultSet rs = uni.Get_Universities(dbParam); 
     int id = 1; 
     while (rs.next()) {  
       String uni_id = rs.getString("id"); 
       String uni_name = rs.getString("name"); 

    %> 
    <tr> 
     <td><%=id%></td> 
     <td><%=uni_id%></a></td> 
     <td><%=uni_name%></td> 
     <td> 
     <div class="material-switch"> 
      <input id="uniOption" name="<%=uni_id%>^uniOption" type="checkbox"/> 
     </div> 
     </td> 
    </tr> 
    <input name="option_count" type="hidden" value="<%=id%>"/> 
     <% id++; 
     } 
     while (rs.previous()) { 
    } 
    %> 
    </tbody> 
    </table> 
    </form> 

입니다 그것은 전체 성공하지. 내가 체크 한 체크 박스에 관계없이 항상 1을 제출합니다.

다음은 내 서블릿에서 얻을 수있는 결과입니다. 모든 체크 박스와 ID가 각각의 체크 박스에 고유 한 속성 것은 이름을 유지하는 것을 의미

<input id="<%=uni_id%>^uniOption" name="uniOption" type="checkbox"/> 

내가 제안 것이다 것은 당신이 같은 코드를 체크 박스 쓰기입니다 enter image description here

답변

0

는 같은 속성. 왜냐하면 자바 스크립트에서 다른 요소는 같은 이름을 가질 수 있지만 ID는 고유해야합니다. 당신은 페이지의 호출을 자바 스크립트 함수를 입력하고 바로 코드 아래의 경우 이제

는 :

var checkboxArray = document.getElementsByName("uniOption"); 

var counter =0; 
for(var i=0;i<checkboxArray.length;i++){ 
if(checkboxArray[i].checked){ 
// here you will get count of all the checked check boxes 
counter++; 
} 
} 

희망이 도움이!

관련 문제