views:

393

answers:

3

I wrote this on a simple ASP.NET page:

jQuery(document).ready(function() {
 jQuery("form").submit(function() {
   alert("kikoo");
   return false
  });
});

and when I submit (by clicking on a button, link, ...), I never see the alert box.

Is there something in asp.net page that "bypass" the submit (I think about the dopostback javascript method)

Can anyone tell me why?

A: 

Is it possible you are confused about the usage of the jQuery submit() function?

It sounds like you're expecting the alert dialog to happen on page load because you think the submit function is actually submitting your form. But with the way you are using it, the submit() function is just attaching an event to your form so that when you actually do submit it, your function will run.

To actually trigger the submit from your code, you have to use submit() without any arguments like this:

$("form").submit();

Here's an expanded example, using Mike C's answer as the basis:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Title</title>
    <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
    $(function() {
        $("form").submit(function() {
            alert("kikoo"); return false
        });
        // you could also chain this onto the end of the previous function
        $("form").submit();
    });
    </script>
</head>
<body>
    <form><input type="submit" /></form>
</body>
</html>
Ryan
The OP explicitly mentions buttons activating the submit and the alert in the posted code is by way of an event handler.
annakata
annakata: sorry, but i don't undestand you answer. It seems my english level is too low.Ryan: I'm no trying to submit the form. I'm trying to bind a behaviour when the form is submited.
Pitming
My apologies. I missed the "by clicking on a button" part. I figure the information in my answer is still useful for people searching though, so I'll leave it up.
Ryan
According to the original poster this is an ASP.NET application, so you need to have <form runat=server>..</form> and probably <asp:button .. /> to replicate this scenario
Herb Caudill
A: 

I found the problem.

when i click on some button, they call a all javascript method that does form.submit. This is not caught by jQuery.

I'll correct the behaviour of my button and it'll work fine.

thanks all for help.

Pitming
Did you find a solution?
Herb Caudill
A: 

Finaly I found that the do postback method override my handler (still don't understand why).

So here is what I did: 1. I declare my behaviour in a JS function

function MyBehaviour()
{...}

2. then i did a "classic" jQuery binding

jQuery(document).ready(function() {
    jQuery("form").submit(EncodeAllTxtControl);
});

3. then on each form, I had myu behaviour but keeping the already declared ones

jQuery("form").each(function() {
        var oldonSubmit = this.onsubmit;
        if (typeof oldonSubmit != 'function') {
            this.onsubmit = function() {
                MyBehaviour();
                return true;
            };
        }
        else
            this.onsubmit = function() {
                MyBehaviour();
                oldonSubmit();
                return true;
            };
    });

And this seems to work.

Pitming