submit()和onsubmit()的區(qū)別(轉(zhuǎn))

          2011-03-16 10:34

          最近在開發(fā)中遇到了表單提交前驗證的問題,用一個普通的button按鈕代替submit按鈕,
          在提交前觸發(fā)這個button的onclick事件,在其事件中觸發(fā)form的submit事件。問題出現(xiàn)了:
          以下是出現(xiàn)相關(guān)代碼:
          <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> 


          卻發(fā)現(xiàn)并沒有觸發(fā)form的onsubmit方法,而是直接提交了。奇怪了,難道沒有這種方式無法結(jié)合form的onsubmit方法嗎?
          仔細(xì)想了想,既然this.form表示form這個對象,那么肯定能獲取到form的屬性和方法的
          ,就改成this.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這個方法是不觸發(fā)onsubmit時間的,如果想要觸發(fā)它,需要調(diào)用
          fireEvent方法。嘗試一下:this.form.fireEvent('onsubmit');哈哈,果然也成功!不過這樣不是多此一舉嗎?呵呵!

          就這個小問題也搞了我將近一個小時,不過為了以后不為這個問題煩惱,這也是值得的。
          this.form.submit(); //直接提交表單
          this.form.onsubmit(); //調(diào)用form的onsubmit方法
          this.form.fireEvent('onsubmit'); //同上,
               PS:又學(xué)到了fireEvent這個方法,

          2.onsubmit()與submit() :

          <sCript>
          funCtion fun()
          {
             alert("form_submit");
          }
          </sCript>

          <form onsubmit="fun()">
          <input type="submit" id="aaa" value="submit">   <!--能彈出form_submit-->
          <input type="button" id="bbb" value="onCliCk_submit" onCliCk="doCument.forms[0].submit()">
          <!--
          表單會提交,但是不會運行fun() 原因是 onsubmit事件不能通過此種方式觸發(fā)(在IE環(huán)境)
          直接用腳本doCumetn.formName.submit()提交表單是不會觸發(fā)表單的onsubmit()事件的
          -->
              <input type="button" id="bb1" value="onCliCk_onsubmit" onCliCk="doCument.forms[0].onsubmit()">

          <!--會觸發(fā)fun()參數(shù)-->
          </form>

          posted on 2011-09-28 15:11 liujg 閱讀(351) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2011年9月>
          28293031123
          45678910
          11121314151617
          18192021222324
          2526272829301
          2345678

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          boddiy

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 连云港市| 东源县| 桑日县| 监利县| 肃宁县| 瑞金市| 碌曲县| 贵港市| 黄浦区| 南充市| 犍为县| 双柏县| 理塘县| 钟山县| 上饶市| 酒泉市| 九寨沟县| 宜州市| 鄱阳县| 怀柔区| 芮城县| 都江堰市| 进贤县| 鲜城| 谷城县| 乌兰浩特市| 越西县| 军事| 鸡东县| 布拖县| 武川县| 广汉市| 南皮县| 洮南市| 永善县| 班玛县| 视频| 枣强县| 通辽市| 锦州市| 巨鹿县|