最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Capturing jQuery form submit event - Stack Overflow

programmeradmin0浏览0评论

I don't know what is wrong with that, because I was following at every step the tutorial from jquery regarding the form submit event.

My Javascript:

[Ofc. latest jQuery library is included].

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

Have also tried with the $(document).ready() event:

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

Here is my HTML form code:

<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 binations, but I fail'd. So I'm waiting for your solutions. I'd appreciate every one.

I don't know what is wrong with that, because I was following at every step the tutorial from jquery. regarding the form submit event.

My Javascript:

[Ofc. latest jQuery library is included].

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

Have also tried with the $(document).ready() event:

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

Here is my HTML form code:

<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 binations, but I fail'd. So I'm waiting for your solutions. I'd appreciate every one.

Share Improve this question edited Dec 6, 2020 at 10:13 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Apr 14, 2012 at 12:02 LucasLucas 3,72514 gold badges49 silver badges77 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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?

P.S. I would alwyas go for the latter code to ensure that the elements are in the DOM before trying to attach events.

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.

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. 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. Still at a loss why .on(submit) or .submit() events will not recognize the form.

$("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.

发布评论

评论列表(0)

  1. 暂无评论