日期:2014-05-16  浏览次数:20361 次

提交表单时,先执行哪一个?
<head>
<script language="javascript">
function hanshu_1()
{
alert("测试中")
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="study_04_root.asp">
  <p>a
  <label for="txta"></label>
  <input onblur="hanshu_1()" type="text" name="txta" id="txta" />
  b
  <label for="txtb"></label>
  <input type="text" name="txtb" id="txtb" />
  c
  <label for="txtc"></label>
  <input type="text" name="txtc" id="txtc" />
  </p>
  <p>
    <input onclick="hanshu_1()" type="submit" name="button1" id="button1" value="提交" />
    <input type="reset" name="button2" id="button2" value="重置" />
  </p>
</form>

</body>
</html>

点用户点击"提交"时
究竟是先执行action="study_04_root.asp",还是先执行onclick="hanshu_1()"

书中说:点击时,会执行form.action
书中又说:有onclick时,会调用相应的函数.

现这二个事情,碰一块了.
究竟先执行哪个呢?

我测试的结论是:先执行函数hanshu_1(),后执行action里面的.
为什么呢?

------解决方案--------------------
先执行点击事件,若事件返回false则不会提交表单
你可以   <input onclick="return hanshu_1()" type="submit" name="button1" id="button1" value="提交" />
测试
------解决方案--------------------
点击提交时,会先执行click事件,如果没有click事件,则执行action
click事件可以决定表单是否最终提交。如果返回false,则不提交,返回true,表单提交