首页 > 编程 > JS > 正文

JS初学者实例教程(6):Document对象读取表单元素

2020-09-19 11:13:39
字体:
来源:转载
供稿:网友

上一篇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 全部选择 提示:你可先修改部分代码,再按运行]

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表