Why doesn't jquery form submit start?

I am trying to submit a form via jquery. I want my form submit object to be launched when jquery submits the form.

But when the form is submitted successfully, submitting the event handler is not successful.

Below is the code:

<html> <head> <title>forms</title> <script src="../common/jquery-1.6.2.min.js"></script> <script> $('#testform').submit(function() { $.post($(this).attr("action"), $(this).serialize(), function(html) { $("#menu").html('<object>'+html+'</object>'); }); return false; // prevent normal submit }); </script> </head> <body> <form id="testform" action="<%=getURL%>" method="post" > <!-- <input type="hidden" value="DocQrySetup" name=form> <input type="hidden" value="bdqdb1" name=config>--> <input type="hidden" value="test" name=otherParams> </form> <script language=javascript> $('#testform').submit(); </script> <div id="menu" style="position:relative; bottom: 0; overflow:hidden;"> </div> </body> 

I searched all forums, but could not get permission.

+6
source share
2 answers

The form does not exist when you run the first script, so your event handler has nothing to attach.

Either you need to move this handler after the form or wrap it in

 $(document).ready(function() { $('#testform').submit(function() { /* your code */ }); }); 
+20
source

The form is not defined when you attach the event listener, move the code into which you attach the event listener after the form, or wrap the code:

 jQuery(document).ready(function ($) { ... }); 

And add a submit button.

 ... <input type="submit" value="submit"> ... 
+7
source

Source: https://habr.com/ru/post/953605/


All Articles