我有一个带有按钮的 HTML 表单:
I have a HTML form with button:
<form action="/" method="post" id="MyForm">
<input type="hidden" name="Field1" value="Value1" />
<input type="hidden" name="Field2" value="Value2" />
<input type="submit" name="name" value="submit" />
</form>
我有提交附加到窗口的事件处理程序:
I have event handler for submit attached to Window:
window.onsubmit = function()
{
alert("Submit happening!");
};
如果我单击提交"按钮,此事件处理程序会正确触发.但是,当从 javascript 以编程方式触发提交时,事件永远不会起作用:
This event handler fires properly if I click "Submit" button. However events never works when submit is triggered programmatically from javascript:
$("#MyForm")[0].submit();
当提交事件处理程序由 Javascript 启动时,如何捕获它,而不是通过用户点击?我尝试使用 Jquery 或 AddEventListener 订阅 Form 事件 - 不起作用.
How to catch submit event handler when it was initiated by Javascript, not by User click? I tried to subscribe for Form event using Jquery or AddEventListener - does not work.
那是因为你不应该只使用 submit 函数,而是 trigger 提交如下:
That's because you shouldn't just use the submit function, but trigger the submit like:
$("#MyForm").trigger('submit');
这篇关于如果提交以编程方式启动,则提交事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 Angular 2/Typescript 中使用 IScrollUse IScroll in Angular 2 / Typescript(在 Angular 2/Typescript 中使用 IScroll)
Anime.js 在 Ionic 3 项目中不起作用anime.js not working in Ionic 3 project(Anime.js 在 Ionic 3 项目中不起作用)
Ionic 3 - 使用异步数据更新 ObservableIonic 3 - Update Observable with Asynchronous Data(Ionic 3 - 使用异步数据更新 Observable)
Angular 2:在本地 .json 文件中找不到文件Angular 2: file not found on local .json file(Angular 2:在本地 .json 文件中找不到文件)
在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指In Ionic 2, how do I create a custom directive that uses Ionic components?(在 Ionic 2 中,如何创建使用 Ionic 组件的自定义指令?)
将 ViewChild 用于动态元素 - Angular 2 &离子2Use ViewChild for dynamic elements - Angular 2 amp; ionic 2(将 ViewChild 用于动态元素 - Angular 2 amp;离子2)