简体   繁体   English

为什么第二个事件监听器不触发?

[英]Why does the second eventlistener not fire?

I have two event listeners on my page, but the second one does not fire when I click the button.我的页面上有两个事件监听器,但是当我单击按钮时第二个没有触发。 How do I get the submit event listener to fire?如何让提交事件侦听器触发?

 document.getElementById("myButton").addEventListener("click", function(e) { e.preventDefault(); document.getElementById("myForm").submit(); }); document.getElementById("myForm").addEventListener("submit", function(e) { e.preventDefault(); console.log("submit triggered") });
 <form id="myForm"> <input><br> <button id="myButton" type="button">Click</button> </form>

According to this link , 'submit' event fires when user submits a form not when you submit a form using script.根据此链接,当用户提交表单而不是您使用脚本提交表单时会触发“提交”事件。

Current browsers do not adhere to this part of the html specification.当前的浏览器不遵守 html 规范的这一部分。 The event only fires when it is activated by a user - and does not fire when activated by code.该事件仅在用户激活时触发 - 并且在由代码激活时不会触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM