submit()とonsubmit()の違い

3042 ワード

最近開発でフォームの提出前に検証する問題に遭遇し、submitボタンの代わりに普通のbuttonボタンを使用しました.
このbuttonのonclickイベントは、コミット前にトリガーされ、formのsubmitイベントはそのイベントでトリガーされます.問題が発生しました.
関連コードは次のとおりです.
Javaコード
  • ストライプコード
  • <form action="http://www.baidu.com/s?wd=this.form.submit%28%29%3B&cl=3" method="post" name="form1" onsubmit="return alert(' !'); return false;">
    	<table align="center" width="420px" cellPadding="2" cellSpacing="1" bgcolor="#A4B6D7"	style="word-wrap:Break-word;">				
    		<tr style="cursor: hand;background:#d7e3f6" >
    			<td width="20%" align="right"> </td>
    			<td><input style="width:90%" type="text" name="GOODSNUM"   size="30"  maxlength="8" ></td>
    		</tr>
    		<tr>
    			<td align="center" colspan="2">
    				<input type="button" name="save" value=" " onclick="if((confirm(' ?'))) this.form.submit();"/>
    			</td>
    		</tr>	
    	</table>
    </form>

    formをトリガするonsubmitメソッドではなく,直接コミットしていることが分かった.おかしいですね.formを結合できないonsubmitの方法はありませんか.
    よく考えてみると、これ以上は.formはformというオブジェクトを表すので、formの属性と方法を必ず取得することができます.
    ああ、これに変更します.form.onsubmit(); 成功!
    マニュアルを調べてみると、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.
    submitという方法はonsubmitイベントをトリガーしないという意味で、トリガーするには呼び出しが必要です
    FireEventメソッド.試してみます:this.form.fireEvent('onsubmit');ははは、やっぱり成功!しかし、これは余計なことではないでしょうか.ほほほ!
    この小さな問題についても1時間近くやったが、後でこの問題に悩まないようにするには、これも価値がある.
    this.form.submit();//フォームの直接送信
    this.form.onsubmit();//formを呼び出すonsubmitメソッド
    this.form.fireEvent('onsubmit');//同じように、
    PS:またfireEventという方法を学びました