恼人的JavaScript“小”问题 —— 避免Form被多次提交

这两天项目里遇到的一个Bug。一个查询表单,输入条件后点submit button或按回车键(无论Form域有没有被Focus)来提交表单。为了处理Form域没有Focus时也要响应KeyPress事件,实现了响应document对象的KeyPress事件的方法,在里面调用Submit的click方法,模拟Submit按钮被点击。但在Form域有Focus的情况下,这样的实现会造成Form被多次提交。

昨天为这个Bug加班到快到零点(一开始居然没想到是Form多次提交的问题,一直在后台Java程序里查来查去,汗。。。)

今天试过用Google发现的一些解决方法,比如(还没有试提交一次后disable按钮的方法)

效果不太好,最终(下班之前)的方案是在使用以上防止多次提交的同时,把Submit按钮换成普通的button,在button的onclick事件和document的onKeyUp事件里调用Form的submit方法,效果还不错。

写这些的时候,查到了disable submit button的方法,明天试试。

还有,找到了dev2dev论坛里一个介绍Structs如何处理多次提交问题的帖子。当然,已经跟javaScript没关系了。项目没有用到Struts,先收藏再说。

Leave a Reply

Your email address will not be published. Required fields are marked *

*

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.
For more help see http://daringfireball.net/projects/markdown/syntax

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>