通过内联JavaScript提交表单时使用提交事件监听器?

问题描述:

我们有一个脚本,通过向表单添加事件监听器来跟踪表单提交。问题是一个客户使用

We have a script that tracks form submits by adding an event listener to the form. The problem is that one customer submits the form via a link using

<a href="javascript:document.formname.submit();">Submit</a>

忽略事件监听器。这是一个JSFiddle来演示: http://jsfiddle.net/XhLkG/

which ignores the event listener. Here's a JSFiddle to demonstrate: http://jsfiddle.net/XhLkG/

正如您所看到的,提交按钮会触发警报,而提交链接只是提交表单并绕过事件监听器。

As you can see the Submit button triggers the alert while the Submit link simply just submits the form and bypasses the event listener.

是否有仍然触发事件监听器的方法?

Is there a way to still trigger the event listener?

编辑:我们无法更改标记,因为它位于我们客户的主页上,他们已经嵌入了我们的脚本。对不起,如果我没说清楚。

We cannot change the markup since it's on our customer's homepage where they have embedded our script. Sorry if I didn't make it clear.

你可以使用这个:

var form = document.getElementsByClassName('form');
form[0].addEventListener("submit", function(e) {

    e.preventDefault();

    alert('event');

});
form[0].childNodes[3].addEventListener("click", function(e) {

    e.preventDefault();

    alert('event');

});