[jQuery] 선택된 radio 버튼 값 가져오는 방법

[jQuery] 선택된 radio 버튼 값 가져오는 방법

알고보면 간단하지만 막상 사용할땐 기억이 나지않아 다시 검색하게 만드는 녀석입니다.

현재 선택된 radio 버튼의 값을 가져와 처리하는 경우가 자주 있습니다. 이때 값을 받아오는 방법을 소개해드립니다.

<script type="text/javascript">
$(document).ready(function(){
   console.log($("input[name=myRadio]:checked").val());
});
</script>

<input type="radio" name="myRadio" value="1" />
<input type="radio" name="myRadio" value="2" checked="checked" />
<input type="radio" name="myRadio" value="3" />
<input type="radio" name="myRadio" value="4" />

위에서 대충 보셔도 아시겠지만 console.log에는 2가 찍히게 됩니다. 여기서 가장 중요한 부분은 셀렉터 뒤에 붙는 :checked 입니다.다. 저는 붕어라서 자꾸 잊어버리네요..

%d 블로거가 이것을 좋아합니다: