document.forms[0].submit() 提交无效ie6 bug

<a/>有个链接属性 href , 而form也有一个页面请求属性 action, 当两属性同时存在时, 点击<a/>当然会优先<a/>的href链接, 但如果是 <a href="javascript:;" ></a>, 又会是什么情况呢? 实践证明IE7.0+ , FF3.0+, Opera9.6+ 均能正确处理, 忽略<a/>的href, 执行表单的submit动作, 唯有 IE6 仍坚持着自己的原则, 只要有 href , 就只尝试执行链接 href .
<form name="form1" id="form1" action="xx.asp" methond="post" ></form>
<a id="submit" href="javascript:void(0);" >提交</a>
<script type="text/javascript">
document.getElementById('submit').onclick = function(){
document.forms[0].submit();
}
</script>

上面这个代码点提交后是无效的,没有任何作用。改成下面这样就可以了

<form name="form1" id="form1" action="xx.asp" methond="post" ></form>
<a id="submit" href="#" >提交</a>
<script type="text/javascript">
document.getElementById('submit').onclick = function(){
document.forms[0].submit();
}
</script>

主要就是将href的属性值"javascript:void(0);"改成"#"就可以了。这个问题一度让我郁闷很久

 
原文地址:https://www.cnblogs.com/fm168/p/2756309.html