Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Jquery form submit event not firing?

I am trying to submit a form through jquery. I want to get my form submit event get fired when jquery submits the form.

But when form is submitted successfully, submit event handler is not called successfully.

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 the forums but was not able to get the resolution.

like image 452
Kshitij Jain Avatar asked Sep 11 '13 06:09

Kshitij Jain


People also ask

Why form is not submitting in jQuery?

The NUMBER ONE error is having ANYTHING with the reserved word submit as ID or NAME in your form. If you plan to call . submit() on the form AND the form has submit as id or name on any form element, then you need to rename that form element, since the form's submit method/handler is shadowed by the name/id attribute.

Why My submit button not working?

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.

Why Onsubmit is not being called?

The onsubmit handler is not called, because the form cannot be submitted by any normal means, i.e. the submit event cannot be caused. There is only one submit control, and it is declared as disabled.

What event triggers when a form is submitted?

The submit event triggers when the form is submitted, it is usually used to validate the form before sending it to the server or to abort the submission and process it in JavaScript. The method form. submit() allows to initiate form sending from JavaScript.


2 Answers

The form doesn't exist when you run the first script so your event handler has nothing to attach to.

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

$(document).ready(function() { 
    $('#testform').submit(function() {
        /* your code */
    });
});
like image 196
Dave Forber Avatar answered Oct 21 '22 02:10

Dave Forber


The form isn't defined when you attach the event listener, move the code where you attach the event listener after the form or wrap the code with:

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

And add a submit button.

...
<input type="submit" value="submit">
...
like image 21
Emil A. Avatar answered Oct 21 '22 03:10

Emil A.