chewie
chewie

Reputation: 333

How to call function after submit form aspx

I want callback alert after submit form, but doesn't worked. Submit not execute.

Only $("#formImpressao").submit(); worked fine.

I try too use $(document).ready(function () { ... :( No Success

Whats wrong?

Sorry for my bad english

<div id="HiddenFormTarget" style="display:none;">
    <form id="formImpressao" method="post" target="frmPrint" action="/VisualizarRelatorios/ImprimirRelatorio.aspx""></form>
</div>
<iframe id="frmPrint" name="frmPrint" width="0" height="0" runat="server">
</iframe>


<script language="javascript" type="text/javascript">
    $("#formImpressao").submit(function () {
       alert('Test');
    });
    $("#formImpressao").submit();

</script>

Upvotes: 0

Views: 842

Answers (4)

user8217724
user8217724

Reputation:

(1) There appears to be an extra quote in your form tag.
(2) Have you tried your approach without the target attribute in the form tag?

e.g. -

<form id="formImpressao" method="post" action="/VisualizarRelatorios/ImprimirRelatorio.aspx"></form>

Upvotes: 0

Petrashka Siarhei
Petrashka Siarhei

Reputation: 732

I reproduce this situation and all right:

[https://codepen.io/piotrazsko/pen/mqMrgo][1]

Upvotes: 0

Hameed Syed
Hameed Syed

Reputation: 4245

<form id="formImpressao" method="post" target="frmPrint" action="/VisualizarRelatorios/ImprimirRelatorio.aspx""></form>
</div>

Now you can use two ways to submit the form using only jQuery,Recommended method

        <script>
        $(document).ready(function(){
          //instead of id selector use tag selector
            $("form").submit(function () {
               alert('Test');
            });
         });
           </script>

Another using id selector which is already posted

Upvotes: 0

Brandon Miller
Brandon Miller

Reputation: 1584

$("#formImpressao").submit(function (e) {
    e.preventDefault();
    alert('Test');
    //Insert AJAX to submit form data
});

e.preventDefault() will prevent the default action of the submit event, and then run your alert, but it will NOT submit the form. For that, you will need to use AJAX. It's simple enough to understand, and there are plenty of SO topics on the use of it, so I won't reiterate. But preventDefault() will get you started.

Upvotes: 2

Related Questions