- 다른 뜻에 대해서는 라디오 문서를 참조하십시오.
- HTML radio tag, radio button
- HTML radio 태그
- HTML 라디오 버튼
- HTML RADIO 버튼
- 라디오 버튼 선택 항목 값 보기
1 자바스크립트[ | ]

html
Copy
<label><input type='radio' name='fruit' value='apple' />사과</label>
<label><input type='radio' name='fruit' value='banana' />바나나</label>
<label><input type='radio' name='fruit' value='lemon' checked='checked' />레몬</label>
<input type='button' value='선택된 항목 확인' onclick='show_checked();'/>
<script>
function show_checked() {
var obj = document.getElementsByName('fruit');
var checked_index = -1;
var checked_value = '';
for( i=0; i<obj.length; i++ ) {
if(obj[i].checked) {
checked_index = i;
checked_value = obj[i].value;
}
}
console.log( '선택된 항목 인덱스=', checked_index, '선택된 항목 값=', checked_value );
}
</script>
2 jQuery[ | ]

html
Copy
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
$(function() {
$("#btn-show-checked").click(function() {
console.log( $("input[name=fruit]:checked").val() );
});
});
</script>
<label><input type='radio' name='fruit' value='apple' />사과</label>
<label><input type='radio' name='fruit' value='banana' checked />바나나</label>
<label><input type='radio' name='fruit' value='lemon' />레몬</label>
<button id='btn-show-checked'>선택된 항목 확인</button>
3 PHP[ | ]
- radio_button.php
html
Copy
<!DOCTYPE html>
<meta charset="utf-8" />
<title>라디오 버튼</title>
<form method='post' action='radio_button_ok.php'>
<input TYPE='radio' id='r1' name='fruit' value='apple' /><label for='r1'>사과</label>
<input TYPE='radio' id='r2' name='fruit' value='banana' /><label for='r2'>바나나</label>
<input TYPE='radio' id='r3' name='fruit' value='lemon' checked='checked' /><label for='r3'>레몬</label>
<input type='submit' value='제출' />
</form>
- radio_button_ok.php
PHP
Copy
<?php
echo '<xmp>';
print_r($_POST);
echo '</xmp>';
?>
- 실행결과
- 바나나를 선택하고 [제출] 누른 경우
text
Copy
Array
(
[fruit] => banana
)
4 같이 보기[ | ]
편집자 Jmnote bot Jmnote
로그인하시면 댓글을 쓸 수 있습니다.