上一篇js教程學習了:javascript初學者實例教程(5):document對象
實例六
本實例主要介紹了document對象讀取表單元素的使用
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> new document </title>
<script language="javascript">
<!--
function firstsubmit(){
alert(document.form1.a01.value);//將表單名為a01的值顯示出來
}
function copyfirstsubmit(){
alert(document.form1.a01.value);
document.form1.a02.value=document.form1.a01.value;//將表單名為a01的值給a02,取a01的值也可以使用、、、//document.form1.elements[0].value
}
//-->
</script>
</head>
<body>
<form name="firstform" method=post action="">
<input type="text" name="firsttext" value="哈啰">
</form>
<form name="secondform" method=post action="">
<input type="text" name="secondtext" value="哈啰">
<input type="submit" name="hehe" value="哈哈">
</form>
<script language="javascript">
<!--
var first = document.firstform.name;
var second = document.secondform.name;
//alert("第一個表單的名字:"+first);//讀取表單元素,將注釋去點即可
//alert("第二個表單的名字:"+second);
//alert("第二個表單的按鈕的name是:"+document.secondform.elements[1].name);
//alert("第二個表單文本域的值:"+document.secondform.elements[0].value);
//alert("第一個文本域:"+document.firstform.firsttext.value);
//-->
</script>
<form name="form1" method=post action="">
a01<input type="text" name="a01"/><input type="button" name="01s" value="提交" onclick="firstsubmit()"/><br/>
a02<input type="text" name="a02"/><input type="button" name="02s" value="提交" onclick="copyfirstsubmit()"/>×在a01中輸入值后再提交
</form>
</body>
</html>
效果演示:
[ctrl+a 全部選擇 提示:你可先修改部分代碼,再按運行]
新聞熱點
疑難解答