<script language="javascript">
function Check1()
{
var form = document.form1;
var check = "";
for (i=0; i<document.getElementsByName("choice1").length; i++)
{
if (form.choice1[i].checked)
{
check = "Y";
var data = form.choice1[i].value;
}
}
if (check != "Y")
{
alert ("선택을 하지 않았습니다.");
}
else
{
alert (data+" 를 선택하셨네요.");
}
}
function Check2()
{
var form = document.form2;
var check = "";
for (i=0; i<form.elements.length; i++)
{
if (form.elements[i].name == "choice2[]")
{
if (form.elements[i].checked == true)
{
check = "Y";
}
}
}
if (check != "Y")
{
alert ("체크박스 선택을 하지 않았습니다.");
}
else
{
alert ("체크박스 선택을 하셨습니다.");
}
}
</script>

<form name='form1'>
<input type="radio" name="choice1" value="A"> A<br>
<input type="radio" name="choice1" value="B"> B<br>
<input type="radio" name="choice1" value="C"> C<br>
<input type="radio" name="choice1" value="D"> D
</form>
<form name='form2'>
<input type="checkbox" name="choice2[]" value="A"> A<br>
<input type="checkbox" name="choice2[]" value="B"> B<br>
<input type="checkbox" name="choice2[]" value="C"> C<br>
<input type="checkbox" name="choice2[]" value="D"> D
</form>

<input type="button" value="라디오" onClick="Check1()">
<input type="button" value="체크박스" onClick="Check2()">

반응형