JSフォーム要素の値の取得

2250 ワード

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title> </title>
</head>
<body>
<form id="form1" name="form1">
  :<input type="text" id="txt" value="txt" />
<br />
 :<input type="radio" value=" " name="sex" />     <input type="radio" value=" " name="sex"  checked="checked"/> 
<br />
 :<input type="checkbox" value="1"  name="check[]"/>1<input type="checkbox" value="2" checked="checked"  name="check[]" />2<input type="checkbox" value="3" checked="checked"  name="check[]" />3
<br />    
 :
<select id="sel" style="width: 100px">
<option value="yi">1</option>
<option value="er" selected="selected">2</option>
<option value="san">3</option>
</select>
<br />
<input type="button" value=" "  onclick="getVal()"/>
</form>

<script type="text/javascript">

function getVal(){
	var str="";
	// 
	var radios = document.form1.sex;
	var n = radios.length;
	for (var i = 0; i < n; i++) {
		if (radios[i].checked) {
			str = "   "+radios[i].value;
		}
	}

	//  1,2,3,4 
	var checkboxs = document.getElementsByName('check[]');
	var n = checkboxs.length;
	for (var i = 0; i < n; i++) {
		if (checkboxs[i].checked) {
			str += "   "+checkboxs[i].value;
		}
	}

	// 
	// :
	str += "    :"+document.getElementById("sel").value;

	// :
	var sel = document.getElementById("sel");
	str += "    :"+sel.options[sel.selectedIndex].value;
	
	alert(str);

}
</script>
</body>
</html>