JavaScript & jQuery & Ajax

[js/jQuery] radio button event (button.checked)

라임오렌지원 2023. 1. 19. 16:39

페이지 진입 시, UI를 상황별로 나눠야 하는 경우가 있어서, 라디오 이벤트를 찾아 봤다.

 

# 라디오 버튼 체크

- Vanilla JavaScript

var btn = document.getElementById('btn');
btn.checked = true;

- jQuery

$('input[name="btn"]').prop('checked', true);

$('input:checkbox').val() == "true";

$('input[type=radio][id=btn]').attr('checked', true);

 

 

# 다른 라디오 버튼 눌렀을 때(바뀔 때) 이벤트

- jQuery

$('input[type=radio][name=btn]').change(function() {
	// method
});

 

 

# 라디오 버튼 value 값 확인

- jQuery

var btnValue = $('input:radio[name=btn]:checked').val();

 

# 라디오 버튼 초기화 ( checked 해제 )

- jQuery

$('input[type=radio]').removeAttr('checked');

참고사이트

https://stackoverflow.com/questions/9476617/how-to-set-radio-button-status-with-javascript

 

How to set radio button status with JavaScript

What method would be best to use to selectively set a single or multiple radio button(s) to a desired setting with JavaScript?

stackoverflow.com

https://blog.edit.kr/entry/jQuery-checkbox-checked-%EB%A5%BC-unchecked-%EB%A7%8C%EB%93%A4%EA%B8%B0

 

[jQuery] checkbox checked 를 unchecked 만들기

라디오 상자를 초기화 하는 것입니다.$("input[type=radio]",frm).removeAttr("checked");$('input[name=foo]').removeAttr('checked') 참고자료: http://www.electrictoolbox.com/check-uncheck-checkbox-jquery/

blog.edit.kr

 

728x90

'JavaScript & jQuery & Ajax' 카테고리의 다른 글

[js/jQuery] jsTree, TreeView  (1) 2023.01.25
[Ajax] ajaxSetup()  (0) 2022.10.05
[js] preventDefault() stopPropagation()  (0) 2022.09.27
정규표현식 정리  (0) 2022.08.25
[JS/jQuery] append(), html()  (0) 2022.08.18