본문 바로가기
study/javascript

javascript,jquery로 반복문 써서 체크박스 모두 체크

by 땅콩잉 2021. 9. 13.
728x90

체크박스는 다음처럼 되어있다.

<input type="checkbox" class="form-check-input " name="check_product[]" id="check_product_숫자" value="숫자">

 

자바스크립트 사용시

document.querySelectorAll("input[name^='check_product[]']").forEach(function(item){
    item.checked=true
});

 

제이쿼리 사용시

$('input:checkbox[name="check_product[]"]').each(function() {
$(this).prop('checked',true);
});

 

결과

모두 체크된다!!

댓글