1. 程式人生 > >form表單的onsubmit() return問題

form表單的onsubmit() return問題

<div class="htmledit_views">                  <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> &nbsp; 以前在專案中為一個表單(from)編寫onsubmit()指令碼的時候,經常需要驗證表單中資料的合法性,所以常會寫道:&lt;form action="/admin/addUser.do" method="post" onsubmit="validateForm();"&gt;,試圖在validateForm()中return false來阻止表單的提交。實際上的效果是即使return false 表單&nbsp;還是會提交。後來發現 onsubmit="return validateForm()"就沒有問題了,當時也沒有多想就繼續做其它事情了。</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> &nbsp;&nbsp;&nbsp;&nbsp; 直到今天看到一篇文章作了清楚的解釋:</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> &nbsp;</p> <p style="line-height:28px;font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> <span style="color:#ff0000;">眾所周知,在表單中加上onsubmit="return false;"可以阻止表單提交。</span></p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 下面是簡單的一小段程式碼:</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> &lt;form action="index.jsp" method="post" onsubmit="submitTest();"&gt;</p> <p style="text-indent:2em;"> &nbsp; &nbsp; &lt;INPUT value="www"&gt;</p> <p style="text-indent:2em;"> &nbsp; &nbsp; &lt;input type="submit" value="submit"&gt;</p> <p style="text-indent:2em;"> &lt;/form&gt;</p> <p style="text-indent:2em;"> &lt;SCRIPT LANGUAGE="JavaScript"&gt;</p> <p style="text-indent:2em;"> &lt;!--</p> <p style="text-indent:2em;"> &nbsp; &nbsp; function submitTest() {</p> <p style="text-indent:2em;"> &nbsp; &nbsp; &nbsp; &nbsp; // 一些邏輯判斷</p> <p style="text-indent:2em;"> &nbsp; &nbsp; <span style="color:#ff0000;">&nbsp;return false;</span></p> <p style="text-indent:2em;"> &nbsp; &nbsp; }</p> <p style="text-indent:2em;"> //--&gt;</p> <p style="text-indent:2em;"> &lt;/SCRIPT&gt;</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 大家判斷像上面的寫法,點選submit按鈕該表單是否提交?</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 若答案為是,就不用往下看了。</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 若答案為否,錯了。實際情況是表單正常提交,若想它不提交,應該將</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> &lt;form action="index.jsp" method="post" onsubmit="submitTest();"&gt;</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 改為</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> &lt;form action="index.jsp" method="post" onsubmit="return submitTest();"&gt;</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 為何?</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 原來onsubmit屬性就像是&lt;form&gt;這個html物件的一個方法名,其值(一字串)就是其方法體,預設返回true;</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 和Java一樣,在該方法體中你可以寫任意多個語句,包括內建函式和自定義函式,如</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> onsubmit="</p> <p style="text-indent:2em;"> &nbsp; &nbsp; alert('haha'); // 內建函式</p> <p style="text-indent:2em;"> &nbsp; &nbsp; submitTest();&nbsp; // 自定義函式</p> <p style="text-indent:2em;"> &nbsp; &nbsp; alert(this.tagName); // 用到了this關鍵詞</p> <p style="text-indent:2em;"> &nbsp; &nbsp; ......(任意多條語句)&nbsp; &nbsp;</p> <p style="text-indent:2em;"> &nbsp; &nbsp; return false;</p> <p style="text-indent:2em;"> "</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 就相當於</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> Form.prototype.onsubmit = function() {</p> <p style="text-indent:2em;"> &nbsp; &nbsp; alert('haha'); // 內建函式</p> <p style="text-indent:2em;"> &nbsp; &nbsp; submitTest();&nbsp; // 自定義函式</p> <p style="text-indent:2em;"> &nbsp; &nbsp; alert(this.tagName); // 用到了this關鍵詞</p> <p style="text-indent:2em;"> &nbsp; &nbsp; ......(任意多條語句)&nbsp; &nbsp;</p> <p style="text-indent:2em;"> &nbsp; &nbsp; return false;</p> <p style="text-indent:2em;"> };</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 這樣的話你就覆寫了(override)其預設方法(預設返回true)</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 大家注意到方法體中可以用this這個關鍵詞,這裡即代表了&lt;form&gt;的物件例項。</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 經過這樣的分析後,以上情況就不難理解了:</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> &lt;form action="index.jsp" method="post" onsubmit="submitTest();"&gt;</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 這樣寫,override方法的效果為:</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> Form.prototype.onsubmit = function() {</p> <p style="text-indent:2em;"> &nbsp; &nbsp; submitTest();</p> <p style="text-indent:2em;"> };</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 在這裡submitTest()雖然返回false,但我們只執行了此函式,沒有對其結果進行任何處理。而</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> &lt;form action="index.jsp" method="post" onsubmit="return submitTest();"&gt;</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> override方法的效果為:</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> Form.prototype.onsubmit = function() {</p> <p style="text-indent:2em;"> &nbsp; &nbsp; return submitTest();</p> <p style="text-indent:2em;"> };</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 這樣,我們利用到了它的返回值,達到了預期效果。</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 這樣理解的話我想印象會深刻得多,就不易出錯了</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 結論:</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 我們可以用Java裡的思維方式來思考模擬JavaScript中的類似情況(JavaScript中基於prototype的面向物件技術也確實是這樣做的),但他們畢竟還是有本質上的區別,如Java是強型別的,有嚴格的語法限制,而JavaScript是鬆散型的。象上述方法:</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> </p> <table cellspacing="1" cellpadding="3" align="center" border="0" style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"><tbody><tr><td>java程式碼:&nbsp;</td> </tr><tr><td> <p> </p> <p style="text-indent:2em;"> Form.prototype.onsubmit = function() {&nbsp;</p> <p style="text-indent:2em;"> };</p> <p style="text-indent:2em;"> </p> </td> </tr></tbody></table><p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;"> </p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 既可以有返回值,又可以沒有返回值,在Java裡是通不過的,畢竟Java裡面不能靠方法的返回值不同來過載(overload)方法,而JavaScript中的過載要鬆散得多。</p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> <br></p> <p style="line-height:28px;color:rgb(61,61,61);font-family:'Hiragino Sans GB W3', 'Hiragino Sans GB', Arial, Helvetica, simsun, u5b8bu4f53;font-size:16px;text-indent:2em;"> 轉載來自:http://blog.163.com/hgj_online/blog/static/476742432007112610135845/</p>             </div>