getElementById与getElementsByName

<!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=gb2312" />
<title>getElementById与getElementsByName</title>
</head>
<script>
function a(){
var num=0;
var str="";
for(var i=0;i<document.form1.checkbox.length;i++){
if(document.form1.checkbox[i].checked){
num+=1;
}
}
if(num==1){
span1.innerHTML="<font color='red'>您选择了一个复选框!</font>"
span1.style.backgroundColor="white"
span1.style.fontSize=14
}
else if(num==2){
span1.innerHTML="<font color='red'>您选择了两个复选框!</font>"
span1.style.backgroundColor="white"
span1.style.fontSize=14

}
else if(num>2){
span1.innerHTML="<font color='red'>您选择了多个复选框!</font>"
span1.style.backgroundColor="green"
span1.style.fontSize=14
}
else{
span1.innerText=""
}

for(var i=0;i<document.form1.checkbox.length;i++){
if(document.form1.checkbox[i].checked==true){
str=str+document.form1.checkbox[i].value+","
}
document.form1.username2.value=str
}

}
</script>
<script>
function check(){
alert(document.forms[0].elements[0].value);
for(var i=0;i<document.getElementsByName("username").length-1;i++){
alert(document.getElementsByName("username")[i].value)
}
return false;
}
</script>
<body>
<form id="form1" name="form1" method="post" action="" onsubmit="javascript:return check();">
  <p>
  <input type="text" name="username" value="名称">
  <input type="text" name="username" value="victor" />
  <input type="text" name="username" value="test" />
  <input type="text" name="username2">
    <span id="username">
 <font color=red>span1</font>
 </span>
    <input type="checkbox" name="checkbox" value="a1" onclick="a()" />
    <input type="checkbox" name="checkbox" value="a2" onclick="a()" /> 
    <input type="checkbox" name="checkbox" value="a3" onclick="a()" />
    <span id="span1">
 </span>
</p>
<input type="submit" value="提交">
  </form>
</body>
</html>

    A+
发布日期:2010年03月07日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: