더 나은 프로그래머가 되자

라디오박스 체크 해제 본문

언어/js

라디오박스 체크 해제

greathuman 2022. 1. 21. 11:07
<input type="radio" name="p_open" value="1"> 공개
<input type="radio" name="p_open" value="2"> 비공개
$(document).on('click','input[type="radio"]',function(){
	thisRadio = $(this);
	if(thisRadio.hasClass("checked")){
		thisRadio.removeClass("checked");
		thisRadio.prop('checked', false);
	}else{
		thisRadio.prop('checked', true);
		thisRadio.addClass("checked");
	};
});

라디오 버튼은 한번 클릭하면 해제가 되지 않는데 체크된 상태로 클릭시 체크가 해제되어야하는 상황이 생겨서 위와 같은 코드로 해결

Comments