form with inline onsubmit is not fired from iframe

时间:2019-01-18 18:56:33

标签: javascript submit onsubmit

I have a form with a inline onsubmit event (that sends a simple alert for testing purposes). That alert fires OK when submiting information from this page.

This form has an iframe which have a button that takes the form parent, and submits it. The "postback" on the parent is firing OK but the alert not. Maybe I'm doing something wrong because the alert is not firing or it cannot be possible?

Parent Form:

<form name="form1" method="post" action="mipagina.aspx" 
onsubmit="javascript:alert('hola');" id="form1">

Iframe JS:

formulario = window.parent.document.forms.item(0);
formulario.submit();

1 个答案:

答案 0 :(得分:1)

这是正常行为。来自MDN

  

HTMLFormElement.submit()方法提交给定的。

     

此方法类似于但不等同于激活表单的commit。但是,当直接调用此方法时:

     

没有引发提交事件。特别是,表单的onsubmit事件处理程序不会运行。   约束验证不会触发。

我可能看到的唯一解决方法是:

  • formulario.submit();
  • 之前添加您的代码
  • 非常不好的主意:覆盖 document.getElementById('form1')。submit 方法(请避免这种情况)