[javaScript] 체크박스 다중 선택 삭제 기능 / 배열

기록지·2022년 4월 15일
0
// 체크박스 선택 후 삭제 버튼 클릭시 이벤트 
function folderDeleteClick(){
  var checkBoxArr = []; 
  $("input:checkbox[name='folderCheckname']:checked").each(function() {
  checkBoxArr.push($(this).val());     // 체크된 것만 값을 뽑아서 배열에 push
  console.log(checkBoxArr);
})

  $.ajax({
      type  : "POST",
      url    : "<c:url value='/folderDelete.do'/>",
      data: {
      checkBoxArr : checkBoxArr        // folder seq 값을 가지고 있음.
      },
      success: function(result){
      	console.log(result);
      },
      error: function(xhr, status, error) {
      	alert(error);
      }  
   });
}

controller

@ResponseBody
@RequestMapping(value="/folderDelete.do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public int folderDelete(HttpServletRequest request, @RequestParam(value="checkBoxArr[]") List<String> checkBoxArr 
, @ModelAttribute("archiveFolder") ArchiveFolder archiveFolder) throws Exception {
    int result = 0;
    String checkNum = "";

    for(String str : checkBoxArr){
    checkNum = str;
    archiveFolder.setFolderSeq(checkNum);
    folderService.archiveFolderDelete(archiveFolder);
    }
  return result;
}
    

출처 : https://truecode-95.tistory.com/70

0개의 댓글