简体   繁体   English

捕获 jQuery 表单提交事件

[英]Capturing jQuery form submit event

I don't know what is wrong with that, because I was following at every step the tutorial from jquery.com regarding the form submit event.我不知道这有什么问题,因为我在每一步都遵循 jquery.com 上关于表单提交事件的教程。

My Javascript:我的Javascript:

[Ofc. [办公室。 latest jQuery library is included].包含最新的 jQuery 库]。

<script type="text/javascript">
$("form#addFav").submit(function(event) { event.preventDefault(); alert("hello"); });
</script>

Have also tried with the $(document).ready() event:也尝试过$(document).ready()事件:

$(document).ready(function(){
$("form#addFav").submit(function(event) { event.preventDefault(); alert("hello"); });
});

Here is my HTML form code:这是我的 HTML 表单代码:

<form action="" id="addFav">
     <input type="text" name="name" class="thin-d"/>
     <input type="submit" value="Send"/>
</form>

So, regarding the above Javascript code, it is supposed to work (I mean it should prevent default action [submitting form] and send the alert then), but it all doesn't work - have tried with thousands of combinations, but I fail'd.所以,关于上面的 Javascript 代码,它应该可以工作(我的意思是它应该阻止默认操作 [提交表单] 然后发送警报),但这一切都不起作用 - 尝试了数千种组合,但我失败了'd。 So I'm waiting for your solutions.所以我在等待你的解决方案。 I'd appreciate every one.我会很感激每一个人。

You probably have some syntax error or somthing like that somewhere else, because what you have just works .你可能在其他地方有一些语法错误或类似的东西,因为你刚刚工作

Are you sure there aren't any JS errors?你确定没有任何 JS 错误?

PS I would alwyas go for the latter code to ensure that the elements are in the DOM before trying to attach events. PS 我总是会使用后面的代码来确保元素在尝试附加事件之前位于 DOM 中。

For anyone else who has the same problem, and still struggling to solve this issue, try to see if you have illegally reused the id , and try changing the form id to something unique .对于遇到同样问题但仍在努力解决此问题的其他任何人,请尝试查看您是否非法重用了 id ,并尝试将表单 id 更改为唯一的内容

I had accidentally given the id to two different DOM elements and the event was being bound to the first element with the respective id and my form was the second one so it was never captured.我不小心将 id 赋予了两个不同的 DOM 元素,并且事件被绑定到具有相应 id 的第一个元素,而我的表单是第二个元素,因此从未被捕获。 This had me pulling my hairs for quiet a long.这让我拉了我的头发安静了很长时间。

I just recently ran into the same issue.我最近遇到了同样的问题。 Jquery on submit would not work on the form, however just changing it to click event worked fine.提交时的 Jquery 在表单上不起作用,但是只需将其更改为单击事件即可。 Still at a loss why .on(submit) or .submit() events will not recognize the form.仍然不知道为什么.on(submit).submit()事件不会识别表单。

 $("form#addFav").click(function (event) { event.preventDefault(); alert("hello"); $(this).submit(); });

this question is old but.. you might have had another submit events firing before yours fired.这个问题很老了,但是……在你的提交事件被触发之前,你可能已经触发了另一个提交事件。 If these other events contained "return false;"如果这些其他事件包含“返回假”; statement then the event execution got interrupted and your code never fired.语句然后事件执行被中断并且您的代码从未被触发。 To put your code BEFORE these events you might use ONSUBMIT form attribute where you can put code that will fire before or at the same time as other events.要在这些事件之前放置您的代码,您可以使用 ONSUBMIT 表单属性,您可以在其中放置将在其他事件之前或同时触发的代码。

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

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