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这个方法,

  • 相关阅读:
    webAPI 控制器(Controller)太多怎么办?
    mysql Unknown error 1054
    .NET MVC项目设置包含Areas中的页面为默认启动页
    EF C# ToPagedList方法 The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must ……
    控制台Main函数传参
    C#编码规范
    Linq扩展方法
    Linq操作ArrayList
    LINQ和文件目录
    LINQ To Objects
  • 原文地址:https://www.cnblogs.com/jiechn/p/3979433.html
Copyright © 2011-2022 走看看