form的submit()方法不能触发onsubmit事件的解决方法,兼容各版本浏览器。
在处理表单提交的时候遇到一个问题,通常用<input type="submit" value="提交" />按钮来提交form,再监听form的onsubmit事件就能在前端处理表单验证的事情。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form id="form" action="http://www.a.com" method="post">
<input type="submit" value="提交" />
</form> <script>
var form =document.getElementById("form");
form.onsubmit = function(){
alert("表单提交了")
}
</script>
</body>
</html>
以上代码很顺利,没有任何问题。
当用别的元素来触发form的提交时确不会触发onsubmit事件。代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form id="form" action="http://www.a.com" method="post">
<input type="submit" value="提交" />
</form>
<a href="javascript:void(0)" id="txtBtn">提交</a>
<script>
var form =document.getElementById("form"),
txtBtn = document.getElementById("txtBtn"); txtBtn.onclick = function(){
form.submit();
}
form.onsubmit = function(){
alert("表单提交了")
}
</script>
</body>
</html>
当点击a链接的提交时以为会弹出:表单提交了,可是没有,但表单内容是提交了的,但没有触发onsubmit事件。查了下手册,原文如下:
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.
关于fireEvent的介绍,可以看这里:http://msdn.microsoft.com/en-us/library/ms536423(v=vs.85).aspx
于是加上fireEvent。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form id="form" action="http://www.a.com" method="post">
<input type="submit" value="提交" />
</form>
<a href="javascript:void(0)" id="txtBtn">提交</a>
<script>
var form =document.getElementById("form"),
txtBtn = document.getElementById("txtBtn"); txtBtn.onclick = function(){
form.fireEvent('onsubmit');
form.submit();
} form.onsubmit = function(){
alert("表单提交了")
} </script>
</body>
</html>
以上代码如愿的弹出:表单提交了,用a标签提交也能触发onsubmit事件了。
到此为止以为OK了,可是在chrome和firefox下依然不行。百度GOOGLE一翻,找到了解决方法,直接上代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form id="form" action="http://www.a.com" method="post">
<input type="submit" value="提交" />
</form>
<a href="javascript:void(0)" id="txtBtn">提交</a>
<script>
var form =document.getElementById("form"),
txtBtn = document.getElementById("txtBtn"); txtBtn.onclick = function(){
var result;
if(form.fireEvent){
//IE浏览器
result=form.fireEvent('onsubmit');
}else{
//FIREFOX\CHROME等标准浏览器
var evt = document.createEvent('HTMLEvents');
evt.initEvent('submit',false,true);
result=form.dispatchEvent(evt);
}
if(result){
form.submit();
} } form.onsubmit = function(){
alert("表单提交了")
} </script>
</body>
</html>
完美解决问题,各浏览器下表现一致了!
关于firefox,chrome等标准浏览器的实现中所用到的方法,具体描述看下面几个链接:
http://www.w3school.com.cn/xmldom/met_document_createevent.asp
http://www.w3school.com.cn/xmldom/met_element_dispatchevent.asp
http://www.w3school.com.cn/jsref/event_initevent.asp
form的submit()方法不能触发onsubmit事件的解决方法,兼容各版本浏览器。的更多相关文章
- vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方式
vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方式 #Region "WPF 当浏览器窗体关闭 ...
- easyui combobox setValue方法不能触发onSelect事件
//setValue方法不能触发onSelect事件 //$("#FundingSource").combobox("setValue", data.Fundi ...
- jquery on绑定事件叠加解决方法
jquery on绑定事件叠加解决方法 代码如下 <pre> $('.maoqiu').each(function () { var is_bind = $(this).attr('is_ ...
- Jquery方法load之后导致js失效解决方法
Jquery方法load之后导致js失效解决方法 >>>>>>>>>>>>>>>>>>> ...
- Jmeter: PATCH方法无法发送参数的暂时解决方法
Jmeter: PATCH方法无法发送参数的暂时解决方法 最近在做API测试,前面的GET这些HTTP Request方法都无压力,顺利解决. 但碰到PATCH方法时,发现无法通过. 通过对比,发现P ...
- [Phonegap+Sencha Touch] 移动开发26 Android下的sencha touch程序,转屏时,Ext.Viewport不能触发orientationchange事件的解决的方法
Sencha touch 2.4.2 已经解决问题了. 比方你为Ext.Viewport的orientationchange事件加入了一个监听方法: Ext.Viewport.on('orientat ...
- 关于ElementUI中MessageBox弹框的取消键盘触发事件(enter,esc)关闭弹窗(执行事件)的解决方法
好久没见了 在项目中遇到一个小小的需求,总结了一下! 详细我就不介绍了,相信大家用过的话,很了解.详见文档-----------> http://element-cn.eleme.io/#/zh ...
- onchange监听input值变化及input隐藏后change事件不触发的原因与解决方法(设置readonly后onchange不起作用的解决方案)
转自:https://www.cnblogs.com/white0710/p/7338456.html 1. onchange事件监听input值变化的使用方法: <input id=" ...
- echarts 图的点击事件(含:点击重复触发的问题及其解决方法)
今天用echarts的时候发现一个问题 鼠标指向不同地市触发一个事件展示该地区趋势图 但是但是后台中不管我第几次鼠标指向都会触发两次指向事件 现在贴出解决办法: 问题完美解决.但是为什么会调用两次, ...
随机推荐
- day05 模块学习
目录 1.模块简介 2.collections模块常见方法 3.random模块 4.time模块 5.pickle模块 6.json模块 7.os模块 8.sys模块 9.正则表达式 10.re模块 ...
- log4j根据包名 日志输出到不同文件中 , service层无法输出日志问题
1. service 层因为要配置事务,使用了代理 <aop:config proxy-target-calss=''true"> <aop:pointcut id=&qu ...
- ubuntu16.04 HyperLedger Fabric 1.2.0 开发环境搭建
安装准备 1. 安装git.cRUL.gcc/g++和make $ sudo apt-get update $ sudo apt-get install build-essential git cur ...
- [UE4]计算箭头方向:正切、SetRelativeRotation、RotationFromXVector、Get MotionController Thumbstick X
正切 正弦函数 sinθ=y/r 余弦函数 cosθ=x/r 正切函数 tanθ=y/x 余切函数 cotθ=x/y 正割函数 secθ=r/x 余割函数 cscθ=r/y 已知y和x,求角度θ: ...
- c#调用python代码
c#调用python的方法比较多,比如ironpython,尽管不用安装python环境,可是不兼容python众多的包,也只更新到了python2,通过创建python进程这种方式可以很好的解决兼容 ...
- AD中修改OU下面用户的属性
第一种方法可行: get-ADuser -searchbase "ou=Wireless,dc=lstech,dc=com" -filter * | set-ADuser -Giv ...
- Condtion type Z123 is mandatory!
user exit: MV45AFZZ->userexit_save_document_prepare ZI_EXIT_SD_CNEMS_SAVE_PREPARE ***Start of ins ...
- Microsoft Speaker Recognition API
azure说话人识别API 官方文档:https://westus.dev.cognitive.microsoft.com/docs/services/563309b6778daf02acc0a508 ...
- quartz.properties完整版
我们通常是通过quartz.properties属性配置文件(默认情况下均使用该文件)结合StdSchedulerFactory 来使用Quartz的.StdSchedulerFactory 会加载属 ...
- 对Spring Ioc的理解
1. 新建一个maven项目,工程目录如下 2. 添加接口MessageService及其实现类MessageServiceImpl MessageService.java package com.p ...