javascript&jquery
jquery checkbox 값 배열로 가져와서 json으로 변경하기
에스크리토
2018. 6. 18. 15:13
반응형
<input id="proid" type="checkbox" name="pid" value="1" />
<input id="proid" type="checkbox" name="pid" value="2" />
<input id="proid" type="checkbox" name="pid" value="3" />
<script>
var total_cnt=0;
var imgArray=new Array();
$('input:checkbox[name="체크박스의 name"]').each(function() {
if(this.checked){//checked 처리된 항목의 값
//alert(this.value);
imgArray[total_cnt]=this.value;//배열로 저장
total_cnt++;
}
});
var jsonimg = JSON.stringify(imgArray);//json으로 바꿈
if(total_cnt==0){
alert('선택하세요');
}else{
console.log(jsonimg);
}
</script>
이런경우가 은근히 많다...
반응형