html form onsubmitフォーム提出の問題


FORMフォームのonclick()、submit()およびonsubmit()の問題


2012-03-01
Tech Notes
252/ 0
最近formデータを処理するフィルタリングに遭遇し、buttonのonclickイベントを使用してチェックしたところ、return falseが見つかり、フォームがコミットされた.
そこで,onclick,onsubmit,submit集合関数の関係と区別を詳細に検討した.
onsubmit
You can override this event by returning false in the event handler. Use this capability to validate data on the client side to prevent invalid data from being submitted to the server. If the event handler is called by the onsubmit attribute of the form object, the code must explicitly request the return value using the return function, and the event handler must provide an explicit return value for each possible code path in the event handler function.
The submit method does not invoke the onsubmit event handler.

submit
The submit method does not invoke the onsubmit event handler. Call the onsubmit event handler directly. When using Microsoft? Internet Explorer 5.5 and later, you can call the fireEvent method with a value of onsubmit in the sEvent parameter.

まずformを生成します
view source
print ? 1 < form action = "#" method = "POST" name = "A" onsubmit = "return X();" > 2 < input type = "text" value = "" /> 3 < input onclick = "Y()" type = "submit" value = " " /> 4 </ form >
X()、Y()関数を自分で書くと、これらの関数の実行順序がわかります.
1) onclick: Y();
2) onsubmit: X();
3) submit();
つまり
onclickがreturn falseを実行しない限りonsubmitを実行し続けます.
onsubmitがreturn falseを持たない限りフォームはコミットされます
もう一つの書き方は「return X();」関数の戻り値を取得できます.そうしないと、関数を呼び出すだけで、戻り値は渡されません.
正しい書き方://X()がfalseを返すとformのsubmitが終了します
エラー表記://X()falseを返した後にonclickイベントに渡されず、formのsubmitが続きます