【参加讨论】 今天有朋友问我关于用javascript来进行页面各表单之间的数据传递的问题,我以前也写过,不过从来没有注意,今天总结了一下,希望能够给大家一些帮助,也帮助我总结以前学过,用过的知识。
一,最简单的就是同一个网页里的表单的数据传递。
举个实例,一个网页上有两个表单,每个表单里一个文本框,一个按钮。点按钮互相对操作对方的文本框的值。我们举的例子是把一个文本框付给另一个文本框。具体的html代码如下:
| <html> <head> <title>untitled document</title> <meta http-equiv="content-type" content="text/html; charset=gb2312"> </head> <body> <form name="form1" method="post" action=""> <input type="text" name="textfield"> <input type="button" name="submit" value="1--------->2" onclick="ok()"> </form> <form name="form2" method="post" action=""> <input type="text" name="textfield2"> <input type="button" name="submit" value="2----->1" onclick="ok1()"> </form> </body> </html> |
以上为html的代码,大家可能注意到了onclik的代码了,有两个函数,接下来就是javascript的代码了:
| <script language="javascript"> function ok() { document.form2.textfield2.value=document.form1.textfield.value; } function ok1() { document.form1.textfield.value=document.form2.textfield2.value; } </script> |