JS中 submit提交與Form表單裡的onsubmit的調用問題?

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   for   div   sp   

最近在開發中遇到了表單提交前驗證的問題,用一個普通的button按鈕代替submit按鈕,
在提交前觸發這個button的onclick事件,在其事件中觸發form的submit事件。問題出現了:
<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方法,而是直接提交了。(自己測得時候並沒有這個情況而是通過onsubmit驗證了)

就改成this.form.onsubmit();  成功!

submit這個方法是不觸發onsubmit事件的,如果想要觸發它,需要調用
fireEvent方法。嘗試一下:this.form.fireEvent(‘onsubmit‘);哈哈,果然也成功!不過這樣不是多此一舉嗎?呵呵!

this.form.submit();     //直接提交表單
this.form.onsubmit();   //調用form的onsubmit方法
this.form.fireEvent(‘onsubmit‘); //同上,
     PS:又學到了fireEvent這個方法,

 

JS中 submit提交與Form表單裡的onsubmit的調用問題?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.