<html>
<head>
<title>表单控件运用完整实例</title>
</head>
<body>
<div>
<table border="1" class="holder" cellspacing="0" width="100%" height="20">
<caption>雇员名单</caption>
<tbody id="personList">
<TR>
<TH>姓名</TH>
<TH>性别</TH>
<TH>籍贯</TH>
<TH>职位</TH>
<TH>简介</TH>
</TR>
</tbody>
</table>
</div>
<hr/>
<div>
姓名:<input type="text" name="name"/><br/>
性别:<input type="radio" name="sex" value="男" checked/>男/<input type="radio" name="sex" value="女"/>女<br/>
籍贯:<select name="nativePlace">
<option value="辽宁" >辽宁</option>
<option value="北京" >北京</option>
<option value="上海" >上海</option>
<option value="湖南" selected>湖南</option>
</select><br/>
职位:<INPUT TYPE="checkbox" NAME="title" value="程序员">程序员</input>
<INPUT TYPE="checkbox" NAME="title" value="高程">高程</input>
<INPUT TYPE="checkbox" NAME="title" value="TL">TL</input>
<INPUT TYPE="checkbox" NAME="title" value="PL">PL</input>
<INPUT TYPE="checkbox" NAME="title" value="GM">GM</input><br/>
简介:<textarea name="intro" rows="10" cols="40"></textarea><br/>
<input type="button" name="btn" value="提交"/><br/>
</div>
</body>
</html>
 <script language="javascript">
<!--

 function $(id) {
return document.getElementById(id);
}

 window.onload=function() {
 $("btn").onclick=function() {
var emp=new Employee(
$("name").value,
new RadioButton("sex").getSelectedValue(),
$("nativePlace").value,
new CheckBoxButton("title").getSelectedValues(),
$("intro").value
);


$("personList").appendChild(emp.getInfoLine());
$("name").value="";
$("intro").value="";
}
}

var sn=0;

 function Employee(name,sex,nativePlace,titles,intro) {
this.name=name;
this.sex=sex;
this.nativePlace=nativePlace;

this.title="";
 for(var i=0;i<titles.length;i++) {
this.title+=titles[i]+",";
}

this.intro=intro;
}

 Employee.prototype.getInfoLine=function() {
var row=document.createElement("tr");
row.setAttribute("height",20);
var cell1=document.createElement("td");
cell1.appendChild(document.createTextNode(this.name));
row.appendChild(cell1);

var cell2=document.createElement("td");
cell2.appendChild(document.createTextNode(this.sex));
row.appendChild(cell2);

var cell3=document.createElement("td");
cell3.appendChild(document.createTextNode(this.nativePlace));
row.appendChild(cell3);

var cell4=document.createElement("td");
cell4.appendChild(document.createTextNode(this.title));
row.appendChild(cell4);

var cell5=document.createElement("td");
cell5.appendChild(document.createTextNode(this.intro));
row.appendChild(cell5);

return row;
}

 /**//*********************************************
* 单选框类
*********************************************/
 function RadioButton(name) {
this.name=name;
}

 RadioButton.prototype.getSelectedValue=function() {
var arr=document.getElementsByTagName("input");

 for(var i=0;i<arr.length;i++) {
 if(arr[i].name==this.name && arr[i].checked) {
return arr[i].value;
}
}

return null;
}

 /**//*********************************************
* 复选框类
*********************************************/
 function CheckBoxButton(name) {
this.name=name;
}

// 得到复选框的选择项,返回值为用户选中的数组,返回空表示用户未选中选项
 CheckBoxButton.prototype.getSelectedValues=function() {
var arr=document.getElementsByTagName("input");
var selectedArr=new Array;
var index=0;

 for(var i=0;i<arr.length;i++) {
 if(arr[i].name==this.name && arr[i].checked) {
selectedArr[index++]=arr[i].value;
}
}

return selectedArr;
}

//-->
</script>


|