"HTML 라디오 버튼"의 두 판 사이의 차이

50번째 줄: 50번째 줄:


==PHP==
==PHP==
===radio_button.php===
;radio_button.php
<source lang='html5'>
<source lang='html5'>
<!DOCTYPE html>
<!DOCTYPE html>
63번째 줄: 63번째 줄:
</source>
</source>


===radio_button_ok.php===
;radio_button_ok.php
<source lang='php'>
<source lang='php'>
<?php
<?php
72번째 줄: 72번째 줄:
</source>
</source>


===예제===
;실행결과
*http://jmnote.com/php/radio_button.php
*예제: http://jmnote.com/php/radio_button.php
 
*바나나를 선택하고 [제출] 누른 경우
*바나나를 선택하고 [제출] 누른 경우
<source lang='text'>
<source lang='text'>

2012년 6월 21일 (목) 11:38 판

HTML RADIO button
HTML 라디오 버튼
HTML RADIO 버튼
라디오 버튼 선택 항목 값 보기

1 자바스크립트

<!DOCTYPE html>
<meta charset="utf-8" />
<title>라디오 버튼</title>
<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='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;
		}
	}
	alert( '선택된 항목 인덱스: '+checked_index+'\n선택된 항목 값: '+checked_value );
}
</script>

2 jQuery

<!DOCTYPE html>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<title>라디오 버튼</title>
<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='button' value='선택된 항목 확인' onclick='show_checked();'/>
<script type="text/javascript">
function show_checked() {
	alert('선택된 항목 인덱스: '+$("input[name='fruit']:checked").index("input[name='fruit']")
		+'\n선택된 항목 값: '+$("input[name='fruit']:checked").val());
}
</script>

3 PHP

radio_button.php
<!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
echo '<xmp>';
print_r($_POST);
echo '</xmp>';
?>
실행결과
Array
(
    [fruit] => banana
)

4 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}