해제 가능한 라디오 버튼 unchecked radiobutton

그룹에서 값을 1개만 선택해야 하는 경우에 type=radio 를 많이 씁니다.

그런데 checkbox와 다르게 radio 타입은 해제가 되지 않습니다.

선택안함이라는 체크박스를 하나 더 넣어도 되지만, 이게  직관적이 않습니다.

"선택안함을 선택함" <-- 좀 이상하죠.

 

그래서 해제가 가능한 radio를 만들어봤습니다. 유용하게 쓰실곳이 있을겁니다.

[code]

<ul>
    <li><input type='radio' name='radioBtn'>체크1</li>
    <li><input type='radio' name='radioBtn'>체크2</li>
    <li><input type='radio' name='radioBtn'>체크3</li>
</ul>

 

<script>

$(document).on("click", "input[name='radioBtn']", function(){
    thisRadio = $(this);
    if (thisRadio.hasClass("imChecked")) {
        thisRadio.removeClass("imChecked");
        thisRadio.prop('checked', false);
    } else { 
        thisRadio.prop('checked', true);
        thisRadio.addClass("imChecked");
    };
})

</script>

[/code]

jsfiddle 은 이쪽 링크

https://jsfiddle.net/6vn5uo0t/1/

 

 

|

댓글 7개

유용하게 사용하겠습니다. 고맙습니다.
감사합니다. 그런데,,버튼 바꿀때 이게 자리바꿈인지 해제인지 구분을 못하니 두번씩 눌러야 하는군요,,ㅋ
팁 감사합니다!
스크립트 부분을 약간 수정해봤습니다

$(document).on("click", "input[name='radioBtn']", function(){
thisRadio = $(this);
if (thisRadio.hasClass("imChecked")) {
thisRadio.prop('checked', false).removeClass("imChecked");
} else {
thisRadio.prop('checked', true);
thisRadio.parent().siblings().find("input[name='radioBtn']").prop('checked', false);
thisRadio.addClass("imChecked").parent().siblings().find("input[name='radioBtn']").removeClass("imChecked");
};
})
와..감사합니다.
wonderful~♥
감사합니다~~~~
댓글을 작성하시려면 로그인이 필요합니다.

그누보드5 팁자료실

+
제목 글쓴이 날짜 조회
6년 전 조회 4,075
6년 전 조회 4,453
6년 전 조회 3,634
6년 전 조회 3,665
6년 전 조회 3,957
6년 전 조회 5,695
6년 전 조회 7,726
6년 전 조회 5,240
6년 전 조회 4,959
6년 전 조회 5,196
6년 전 조회 4,075
6년 전 조회 5,028
6년 전 조회 3,143
6년 전 조회 6,442
6년 전 조회 5,960
6년 전 조회 1.2만
6년 전 조회 5,185
6년 전 조회 4,155
6년 전 조회 4,804
6년 전 조회 4,739
6년 전 조회 3,508
6년 전 조회 3,700
6년 전 조회 5,192
6년 전 조회 5,379
6년 전 조회 7,409
6년 전 조회 4,501
6년 전 조회 6,848
6년 전 조회 4,518
6년 전 조회 1.2만
6년 전 조회 4,793