관리 메뉴

웹개발 블로그

[jQuery] 체크박스 배열 유효성 검사 본문

◆JavaScript/jQuery

[jQuery] 체크박스 배열 유효성 검사

쿠키린 2023. 2. 3. 15:55

https://hyeonddobbi.tistory.com/82

 

[jquery] 라디오버튼 또는 체크박스 값 가져오기 및 체크 해제하기

https://electronic-moongchi.tistory.com/42 [jQuery] 라디오(radio) 버튼, 체크박스(checkbox) 선택/해제 하는 방법 [jQuery] 라디오(radio) 버튼, 체크박스(checkbox) 선택/해제 하는 방법 일반적으로 라디오버튼과 체크

hyeonddobbi.tistory.com

<input type='checkbox' name='tmpChkbox[]' value='A' checked>
<input type='checkbox' name='tmpChkbox[]' value='B' checked>
<input type='checkbox' name='tmpChkbox[]' value='C'>
<button type="button" onclick="clkBtn()">체크된 값 배열로 생성</button>

<script type="text/javascript">
  function clkBtn(){
    var chkArray = new Array();

    $("input[name='tmpChkbox[]']:checked").each(function() { 
      var tmpVal = $(this).val(); 
      chkArray.push(tmpVal);
    });

    if( chkArray.length < 1 ){
      alert("값을 선택해주시기 바랍니다.");
      return;
    }

    console.log(chkArray);	// (2) ["A", "B"]
  }
</script>