해제 가능한 라디오 버튼 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,015
6년 전 조회 4,391
6년 전 조회 3,577
6년 전 조회 3,601
6년 전 조회 3,891
6년 전 조회 5,620
6년 전 조회 7,658
6년 전 조회 5,160
6년 전 조회 4,859
6년 전 조회 5,120
6년 전 조회 4,013
6년 전 조회 4,972
6년 전 조회 3,081
6년 전 조회 6,366
6년 전 조회 5,882
6년 전 조회 1.1만
6년 전 조회 5,115
6년 전 조회 4,094
6년 전 조회 4,749
6년 전 조회 4,677
6년 전 조회 3,453
6년 전 조회 3,641
6년 전 조회 5,138
6년 전 조회 5,326
6년 전 조회 7,334
6년 전 조회 4,441
6년 전 조회 6,770
6년 전 조회 4,455
6년 전 조회 1.2만
6년 전 조회 4,715