简体   繁体   中英

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.

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 */
    });
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM