zoukankan      html  css  js  c++  java
  • JS中 submit提交与Form表单里的onsubmit的调用问题?

    最近在开发中遇到了表单提交前验证的问题,用一个普通的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="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这个方法,

  • 相关阅读:
    使用dbms_metadata.get_ddl遇到ORA-31603
    oracle得到建表语句
    Linux性能优化和监控系列(一)——top工具
    Oracle在Linux下的性能优化
    地址跳转问题
    无法对 数据库'UDS' 执行 删除,因为它正用于复制
    [.net 多线程]SpinWait
    [.net 多线程 ]ReaderWriterLock
    [.net 多线程]Barrier
    [.net 多线程]CountdownEvent
  • 原文地址:https://www.cnblogs.com/jiechn/p/3979433.html
Copyright © 2011-2022 走看看