免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2696 | 回复: 0
打印 上一主题 下一主题

html 的 image button 自动提交 form [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-11 16:35 |只看该作者 |倒序浏览
html 的 image button 自动提交 form


今天在项目的代码中看到这么一段:
<HTML>
<BODY>
<FORM name="frmData" onsubmit="Login()">
<INPUT type="image" src="submit.gif" name="imgSubmit" value="" />
</FORM>
</BODY>
<script>
function Login()
{
  alert('hello');
}
</script>
</HTML>
奇怪了,这个登陆按钮没写任何代码触发 form 的提交,但是我们的程序又是怎样正确 work 的?
我想是不是在另外的地方用 js 给这个按钮绑定了 onclick 事件?
于是我把代码拷下来,生成了上面的简化版.
在测试中点击图片后,成功弹出 hello.
查阅资料发现,image button 可以自动触发 form 的 onsubmit 事件.
另外通过用 button 测试,还发现一个现象:
<input type="button" name="btnSubmit" value="submit" onclick="frmData.submit()"/>
这样 form 是提交了,但是不会进入 Login 方法.
相关资料解释:
The submit method does not invoke the onsubmit event handler. Call the onsubmit event handler directly. When using Microsoft&reg; Internet Explorer 5.5 and later, you can call the fireEvent method with a value of onsubmit in the sEvent parameter.
要这样才能调用到 Login 方法:
<input type="button" name="btnSubmit" value="submit" onclick="frmData.fireEvent('onsubmit')"/>
或者
<input type="button" name="btnSubmit" value="submit" onclick="frmData.onsubmit()"/>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP