javascript - Using on() to trigger a form submit - Stack Overflow

I am trying to bind this submit event from a form which not always be in Dom, so I trying with .on():$(

I am trying to bind this submit event from a form which not always be in Dom, so I trying with .on():

$('body').on("form","submit", function(e){})

but firebug logs:

$("body").on is not a function
[Parar en este error]

$('body').on("form","submit", function(e){

tried also

$(document).on("form","submit", function(e){})

I am trying to bind this submit event from a form which not always be in Dom, so I trying with .on():

$('body').on("form","submit", function(e){})

but firebug logs:

$("body").on is not a function
[Parar en este error]

$('body').on("form","submit", function(e){

tried also

$(document).on("form","submit", function(e){})
Share Improve this question edited Oct 27, 2019 at 16:30 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 11, 2012 at 19:02 Toni Michel CaubetToni Michel Caubet 20.2k58 gold badges218 silver badges388 bronze badges 4
  • 3 Are you using jQuery 1.7 or newer? Is jQuery loaded when you run that code? You need to swap form and submit around as well. – Niklas Commented Mar 11, 2012 at 19:05
  • You are probably using an old version of jQuery and you also need to change the order of the selector and the event. – Bruno Silva Commented Mar 11, 2012 at 19:06
  • omg! that is it! i am using <1.7... thanks! – Toni Michel Caubet Commented Mar 11, 2012 at 19:07
  • @ToniMichelCaubet If you are not interested in upgrading, and you are using 1.4.2 or newer, api.jquery./delegate might be right for you – Niklas Commented Mar 11, 2012 at 19:08
Add a ment  | 

2 Answers 2

Reset to default 9

If you are upgrading to jQuery 1.7, you can use on() by applying form as a selector, like:

$('body').on("submit", "form", function(e){

Which will bind the submit event to body, but filtering it to form which works the same way as the delegate example below.

If you are using 1.4.2 or newer, you can use delegate() like this:

$('body').delegate("form", "submit", function(e){

You have to select the form and use on to bind the event listener.

$( "form" ).on( "submit", function(e){} );

You also need jQuery version 1.7 or higher to use on.

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

相关推荐

  • javascript - Using on() to trigger a form submit - Stack Overflow

    I am trying to bind this submit event from a form which not always be in Dom, so I trying with .on():$(

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信