2015-01-26 53 views
0

我试图通过另一种形式定义的按钮提交窗体时出现问题。多个窗体的交互

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Test submit</title> 
</head> 
<body> 
    <form id="dummyForm" name="dummyForm"> 
    <button onclick="sendTheForm();">Submit other form</button> 
    </form> 
    <form id="formID" name="formName" action="viewpost.php" method="POST"> 
    <input type="hidden" value="something" id="inputA1" name="inputA1" /> 
    <input type="hidden" value="something" id="inputA2" name="inputA2" /> 
    </form> 
    <script type="text/javascript"> 
    function sendTheForm() { 
    document.forms['formName'].submit(); 
    } 
    </script> 
</body> 
</html> 

实现重定向按预期的方式更改以下行的唯一方法:我不明白的是,表单提交这样工作

onclick="sendTheForm();return false;" 

。谁能解释一下?

提前致谢!

回答

2
  1. 您点击提交按钮
  2. 的JavaScript事件处理程序触发
  3. 另一种形式开始提交
  4. 提交按钮执行其默认动作
  5. 第一种形式提交,并取消了其他形式的提交过程
+0

特定的按钮不是提交类型,所以我不明白为什么虚拟表单首先提交触发器。 **按钮的默认动作是什么?**答案:[按钮参考](http://w3c.github.io/html-reference/button.html) – nhereveri 2015-01-27 01:32:41

+0

@nhereveri - 它**是**提交类型。您指向的链接说:*没有指定type属性的button元素表示与type属性设置为“submit”的button元素相同的东西* – Quentin 2015-01-27 10:32:06

+0

是的。几分钟后,我检查了第二步。 – nhereveri 2015-01-27 20:49:25

0

我从this post得到正确答案感谢Quentin t IPS。 为什么首先提交触发器?导致默认类型的按钮它是一个提交类型,但不同的浏览器使用不同的默认类型。检查Button Reference

下一行按预期工作,无返回false诡计!

<button type="button" onclick="sendTheForm();">Submit other form</button> 
+0

'