Web/jQuery

jQuery에서 radio버튼 처리 checked - radio 값 가져오기

saltdoll 2017. 9. 13. 03:53
반응형

Radio 체크 박스 선택하기

https://stackoverflow.com/questions/426258/setting-checked-for-a-checkbox-with-jquery

jQuery 1.6+

Use the new .prop() method:

$('.myCheckbox').prop('checked', true);
$('.myCheckbox').prop('checked', false);

jQuery 1.5.x and below

The .prop() method is not available, so you need to use .attr().

$('.myCheckbox').attr('checked', true);
$('.myCheckbox').attr('checked', false);

Note that this is the approach used by jQuery's unit tests prior to version 1.6 and is preferable to using

$('.myCheckbox').removeAttr('checked');

since the latter will, if the box was initially checked, change the behaviour of a call to .reset() on any form that contains it - a subtle but probably unwelcome behaviour change.

For more context, some incomplete discussion of the changes to the handling of the checkedattribute/property in the transition from 1.5.x to 1.6 can be found in the version 1.6 release notesand the Attributes vs. Properties section of the .prop() documentation.

Any version of jQuery

If you're working with just one element, you can always just modify the HTMLInputElement's .checked property:

$('.myCheckbox')[0].checked = true;
$('.myCheckbox')[0].checked = false;

The benefit to using the .prop() and .attr() methods instead of this is that they will operate on all matched elements.

 

 

Radio 체크 박스 값 가져오기

https://stackoverflow.com/questions/596351/how-can-i-know-which-radio-button-is-selected-via-jquery

To get the value of the selected radioName item of a form with id myForm:

$('input[name=radioName]:checked', '#myForm').val()

Here's an example:

 

$('#myForm input').on('change', function() {
   alert($('input[name=radioName]:checked', '#myForm').val()); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myForm">
<input type="radio" name="radioName" value="1" /> 1 <br />
<input type="radio" name="radioName" value="2" /> 2 <br />
<input type="radio" name="radioName" value="3" /> 3 <br />
</form>

 

예제> rdaio버튼 가져오기

<input name="name_of_your_radiobutton" value="YES" checked="checked">
<input name="name_of_your_radiobutton" value="NO">

$('input[name=name_of_your_radiobutton]:checked').val();

 

YES를 가져오겠죠.

참고: https://stackoverflow.com/questions/8622336/jquery-get-value-of-selected-radio-button

 

 

반응형
도움이 되셨다면 하트모양의 "♡ 공감"을 눌러주시면 큰 격려가 됩니다.
(로그인하지 않으셔도 가능)