Readers: 665 | Updated: 2008

使用HTML中form表单组件时的注意事项

Translate Into:

作者: oasis-sun  链接:http://oasis-sun.javaeye.com/blog/201790  发表时间: 2008年06月10日

声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!

一、安全

      出于安全考虑,浏览器在下面两种情形之下,会拒绝执行通过客户端脚本对form组件submit()方法的调用:

  • 直接通过客户端脚本调用INPUT type="file"组件的click()方法,选择文件并返回
  • 通过程序预先设置INPUT type="file"组件的value属性

      正常情况:

  • 上传文件必须通过用户点击INPUT type="file"组件的[浏览…]按钮,选择好待上传文件返回后,再提交表单。也就是说上传文件是在用户知情,且通过用户授权的,才可以上传。

      而在前面两种情况下,待上传的文件都是程序指定,未经过用户授权的,更有甚者是在用户不知情的情况下。如果允许提交表单的话,那么客户端的重要系统文件岂不是可以随意上传到服务器(居心叵测的服务器)上。正是因为这样浏览器才会拒绝执行上面两种情形下通过客户端脚本对form组件submit()方法的调用。

 

二、通过name属性,命名表单和表单元素

      1、通过name属性,获取表单和表单元素的引用

 

      Form对象的引用,通常可以从document.forms[]数组中获得。如document.forms[0]就是文档中的第一个表单。
而包含在Form对象中的表单元素的引用,通常可以从document.forms[].elements[]数组中获得。如document.forms[0].elements[2]就是文档中的第一个表单中的第三个表单元素。
      通过设置form以及各种表单元素的name属性,引用将变得方便得多。

      如下面的表单:

<form name="tform1" enctype="multipart/form-data" method="POST" action="upload.jsp" target="myiframe">     <INPUT type="hidden" name="hideIdentify" value="upload" /> </form>

 

      对表单和表单元素的引用如下:

document.tform1 document.tform1.hideIdentify

 

      2、通过target属性,将表单提交结果返回到同页面中的iframe

 

      对于form的target属性,如果想将表单提交后的结果返回到同页面中的iframe中,须将target属性设置为该iframe的name属性。

<iframe name="myiframe" src="javascript:false"></iframe>

 

      3、上传表单内容

 

      表单中各表单元素的值是以 “表单元素name = 表单元素value” 的形式上传到服务器的。因此request.getParameter(name)方法中的name参数值应为 [表单元素name] 。

三、方法submit()和reset()

      通过点击表单的提交按钮,提交表单之前会触发onsubmit事件handler。而通过客户端脚本调用form对象的submit()方法,只是提交表单,但不会触发onsubmit事件handler。
      同样,通过点击表单的重置按钮,重置表单之前会触发onreset事件handler。而通过客户端脚本调用form对象的reset()方法,只是重置表单,但不会触发onreset事件handler。
      如果onsubmit事件handler返回false,会阻止表单的提交。
      同样,如果onreset事件handler返回false,会阻止表单的重置。


本文的讨论也很精彩,浏览讨论>>


JavaEye推荐



From The Blogs

Internet Observation

2007
给样式表单添加Vista字体
几个月前Christian Montoya写了一篇关于给样式表添加Vista字体的文章。我目前只添加了Calibri,就发现效果果然不错。下面我就介绍一下标题字体Calibri与Arial的比较:Ca... 查看全文

Download Squad

2008
Compile stats for text and HTML docs with Text Stat
Filed under: Utilities, Windows, Open Source Most word processors will give you some basic statistics about documents you're working on, like a word count. But what if you want to see how many words, ... 查看全文

Vandelay Website Design

2008
40 Options for Converting PSD to HTML
Im sure youve seen advertisements for a few different services that will convert your PSD files into HTML and CSS. If youre looking to save some time on your next project you may want to consider tryi... 查看全文

MakeUseOf.com

2008
HTML 2 PDF : Convert any Web page to PDF
Htm2pdf is an HTML to PDF converter that lets you convert web pages to PDF and save them on your computer. Simply type in the address of the page (or copyn paste the html code directly) and let it con... 查看全文

Lifehacker

2008
HTML Trick to Add Page Numbers to Google Docs [Google Docs]
The Google Operating System blog provides a few blocks of code that anyone can insert into their Google Docs word processing files to add dynamic page numbers to page headers and footers, but which sh... 查看全文

Lifehacker

2008
HTML Trick to Add Page Numbers to Google Docs [Google Docs]
The Google Operating System blog provides a few blocks of code that anyone can insert into their Google Docs word processing files to add dynamic page numbers to page headers and footers, but which sh... 查看全文

Lifehacker

2008
HTML Trick to Add Page Numbers to Google Docs [Google Docs]
The Google Operating System blog provides a few blocks of code that anyone can insert into their Google Docs word processing files to add dynamic page numbers to page headers and footers, but which sh... 查看全文

Wangtam

2008
Foigo: 在线的投票调查,表单与数据库
Foigo 是一款应用范畴比较广泛的在线工具,可支持用户在线进行投票调查、表单制作与数据库的创建、管理。这是一款相当适合于企业级应用的在线工具,可方便打造各类市场营销的辅助应用、低成本的数据库录入和日... 查看全文

郭爽的Blog

2008
批处理查找替换大量html文件办法
以前在windows下都是用 editplus,但 editplus的替换只能是打开的文件,“在文件夹中寻找”没有替换功能。这次是6000个html文件,editplus 甚至连 1000 个都开不了... 查看全文

C++博客-首页原创精华区

2008
批量html转text
摘要: 原来的代码是参考“Recipe 12.11. Using MSHTML to Parse XML or HTML”,利用htmlfile提取文本。将当前目录下的所有html文件转换为text文... 查看全文
More Articles