上一篇js教程學習了:js初學者實例教程(7):注冊表單驗證
實例八
本實例主要介紹了document對象讀取表單元素的使用(單選按鈕、復選按鈕的使用)
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> new document </title>
<meta name="generator" content="editplus">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">
<script language="javascript">
<!--
function sel_coun(){
var country = document.form1.country.length; //得到radio個數
for(var i=0;i<country;i++){
if(form1.country[i].checked){
alert(form1.country[i].value);
break;
}else{
continue;
}
}
}
function sel_love(){
var country = document.form1.love.length; //得到checkbox個數
var love =""; //new array();
for(var i=0;i<country;i++){
if(form1.love[i].checked){
love+=form1.love[i].value+"、";
}
}
love = love.substring(0,love.lastindexof("、"));
alert("你的愛好有:"+love)
}
//-->
</script>
</head>
<body>
<form name ="form1" method=post action="">
單選應用
<input type="radio" name="country" value="中國" checked>中國
<input type="radio" name="country" value="法國">法國
<input type="radio" name="country" value="美國">美國
<input type="button" value="提交" onclick="sel_coun();"/><br/>
復選應用
<input type="checkbox" name="love" value="打球">打球
<input type="checkbox" name="love" value="游泳">游泳
<input type="checkbox" name="love" value="看書">看書
<input type="checkbox" name="love" value="跳舞">跳舞
<input type="button" value="提交" onclick="sel_love();"/><br/>
</form>
</body>
</html>
效果演示:
[ctrl+a 全部選擇 提示:你可先修改部分代碼,再按運行]
新聞熱點
疑難解答