tags:

views:

129

answers:

2

I'm trying to use jQuery to submit a form so the page doesn't need to be reloaded I cant get it to hook onto the form though.

<form action="index.php" method="get" id="frmFilter">

and

$("#frmFilter").submit( function () {
    alert('t');
    return false;
});

It just won't work though; when I use the submit button on the form it submits instead of showing an alert.

The form is actually loaded using jQuery's .load event and I have tried to put the above code on the page with the form as well as the page that loads the form and both wouldn't work.

Any idea what I'm doing wrong?

Tested using both IE8 and Chrome 3.

A: 

In your callback:

$("#frmFilter").submit( function (e) {
    e.preventDefault();
    alert('t');
    return false;
});
Jeff Ober
That din't work either.
Hintswen
+3  A: 

You mentioned that you are dumping the form onto your page with jquery's load method. This means that any kind of submit event handler fired in the "parent page's" document.ready will have already fired by the time your load finishes its job. What you need to do is find a way to attach this event after the load has finished... There are two ways I can suggest:

One, in your "parent page", attach the event after the load has completed, in a callback. For example:

$('#sometarget').load('someform.php', function() {
    // this is the load "complete" callback. attach events in here.
    $("#frmFilter").submit( function () {
        alert('t');
        return false;
    });
});

Another idea is to put this script in the "someform.php" (or whatever you're calling it!), but as part of the document, not in its head or own "document.ready". (Remember, document.ready fired long ago, so anything in this "someform" page's own "document.ready" will never see the light of day when being injected via load...)

PARENT PAGE

$('#sometarget').load('someform.php');

FORM PAGE

<form action="index.php" method="get" id="frmFilter">
    <input type="..." />
    <input type="..." />
</form>
<script type="text/javascript">
$("#frmFilter").submit( function () {
    alert('t');
    return false;
});
</script>

Good luck!

Funka
I think you need another argument between the url and the callback like: $('#target').load('url', {}, function() { });http://docs.jquery.com/Ajax/load
Akrikos
still nothing >.<
Hintswen
are there any other javascript errors on the page? If you have firebug installed, keep your eye out for a red X in the corner as the page is submitting...
Funka
@Akrikos - the param you mention is optional. I suppose there isn't any harm in submitting `{}`, but it isn't necessary
Funka
firebug reports no errors.
Hintswen
Could you show us the code you have where you perform the `load` ?? You wouldn't happen to be using a selector as part of the URL, would you? (i.e., separated from the URL by a single space)?
Funka
@Funka - right! I forgot about how jQuery handles optional params. The first way you suggest should work, but the second won't: check out line 3272 of http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js - jquery removes any scripts from the fragment being loaded.
Akrikos
@Akrikos - indeed! This was killing me earlier; it is not mentioned in the docs nor had I come across it before. See my question here : http://stackoverflow.com/questions/1369744/how-to-get-scripts-to-fire-that-are-embedded-in-content-retrieved-via-the-jquery
Funka
@Funka - I am actually suing a selector but I've made sure everything is inside it@Akrikos - removed the selector an it works now! Good thing the file that has that script doesn't need to use the selector.
Hintswen