javascript - Capturing jQuery form submit event - Stack Overflow

I don't know what is wrong with that, because I was following at every step the tutorial from jque

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742415872a4439733.html

相关推荐

  • javascript - Capturing jQuery form submit event - Stack Overflow

    I don't know what is wrong with that, because I was following at every step the tutorial from jque

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信