原来form表单不止可以直接在html中输入,还可以在JS中输入。
<pre name="code" class="html"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>childpage2</title>
<script language="javascript">
function func2_8(){
var mychoice="";
for(var i=0;i<document.form1.ah.length;i++){
//写的时候要仔细,一个小错误就会导致运行失败
if(document.form1.ah[i].checked) mychoice=mychoice+document.form1.ah[i].value+"、"; }
alert("你的选择是:"+mychoice);
}
function func2_9(){
var xm=""; var nl=""; var dw="";
xm=document.form2.elements["xm"].value;
nl=document.form2.elements["nl"].value;
dw=document.form2.elements["dw"].value;
alert("你填写的是:\n\n姓名:"+xm+"\n年龄:"+nl+"\n单位:"+dw); }
</script>
</head>
<body>
我是第二个主页
<input type="button" name="submit" value="click me" οnclick="javascript:alert('hello,Im page2');" >
<hr>
<form name="form1" method="post" action="">
爱好:<br>
<input type="checkbox" name="ah" value="运动">运动
<input type="checkbox" name="ah" value="音乐">音乐
<input type="checkbox" name="ah" value="旅游">旅游<br>
<input type="button" name="Submit" value="看看我选择了什么" onClick="func2_8();">
</form>
<form name="form2" method="post" action="">
姓名:<input type="text" name="xm"><br>
年龄:<input type="text" name="nl"><br>
单位:<input type="text" name="dw"><br>
<input type="button" name="Submit" value="看看我填写了什么" onClick="func2_9();">
</form>
</body>
</html>
表单可以编号,注意写代码的习惯要好,functions和form的命名要有序。