# 웹 개발/프레임워크

jsp 체크박스 선택하기 로직

dev-jjong 2013. 9. 4. 10:05

송신측 소스

<html>
<head>
<script language="javascript" type="text/javascript">
 function checkAll(checkList, boolCheck) {
  varchkSize = 0;
  checkedCount = checkList.length;

  if (typeof (checkedCount) != "undefined") {
   for ( var i = 0; i < chkSize; i++) {
    checkList[i].checked = boolCheck;
   }
  }
 }

 function checkDel() {
  var chkFirList = document.getElementsByName('check');
  var arrFir = new Array();
  var cnt = 0;
  for ( var idx = chkFirList.length - 1; 0 <= idx; idx--) {
   if (chkFirList[idx].checked) {
    arrFir[cnt] = chkFirList[idx].value;
    cnt++;
   }
  }
  if (arrFir.length != 0) {
   document.form1.submit();
  } else {
   alert('삭제할 **를 선택하세요.');
   return;
  }
 }
</script>
</head>
<body>
 <form name="form1" method="post" action="formprocess.jsp" id="checkList">
  <input type="checkbox" onclick="javascript:checkAll(document.getElementsByName('check'), this.checked);" />전체선택
  <input type="checkbox" name="check" value="1" /> 1
   <input type="checkbox" name="check" value="2" /> 2 
   <input type="checkbox" name="check" value="3" /> 3 
   <input type="checkbox" name="check" value="4" /> 4 
   <input type="checkbox" name="check" value="5" /> 5 
   <input type="button" value="삭제" onclick="checkDel();">
 </form>
</body>
</html>



수신측 소스

<html>
<head>
</head>
<body>
 <%
  String[] checks = request.getParameterValues("check");
 
  int checklen = checks.length;
  
  try {
   for(int i=0; i<checklen; i++) {
    out.println(checks[i]+";");
   }
  }
  catch(Exception ex) {
   out.println("exception occured");
  }
 %>
</body>
</html>