views:

13

answers:

2

Hi there

I have a page with a div that is dynamically filled using a paginator ;-) At page init I load the first 10 forms in it using jquery .load() method.

What I'd like to do is to make all the forms dynamically updatable using AjaxForm plugin. I know about server side, let's call it update.asp. It works.

But there are several questions:

  1. How to make plugin work in the first place as the AjaxForm seems not to work to the firms inside a dynamically loaded div?

  2. How do I ID and name the forms? Now I used ID and name myForm to all of them (maybe that is why it doesn't work). Because if I use name myForm1, myForm2 etc... I have to write 10 ajaxForm functions that I use:

         $('#myForm').ajaxForm({
           beforeSubmit: showLoader,
           success: hideLoader
     }); 
    

I would then need to make this 10 times using myForm1 to myForm10? There must be another way...

  1. How do I make AjaxForm work with the pages that are not loaded yet? I think this is the same problem as 1). Because even page 1 is loaded dynamically somehow the ajaxForm doesn't get bind to the form.

Sorry, I am quite new to jquery, I am trying hard to study it, I tried this quite some time before I wrote here. If you can help me, I'd be very gratefull.

Yours

Jerry

EDIT: Here is my loader now... It is not working OK, as the loader is never shown, it dissapears so fast I can see it only if I put alert in the hideLoader :-(((

      function load(num){
      showLoader2();
      var link='/obdelaneslike.asp?ID=<%=request.QueryString("IDRecept") %>&offset='+ num
       $('#content').load(link, function(){
        hideLoader2();
        $('.ajax-loader').hide();

         $('.myForm').bind("submit", function(event) { 
                   $(this).ajaxForm({

               beforeSubmit: showLoader($(this).find('img.ajax-loader').attr('id')),
           success: hideLoader($(this).find('img.ajax-loader').attr('id'))

           }); 
           return false;
                  }); 

           });

       }
A: 

ID values are unique to a single DOM element. So you'd need to give each form a new ID, so if you had three forms, you could name them like so:

<form name="formone" id="formone"...
<form name="formtwo" id="formtwo"...
<form name="formthree" id="formthree"...

Now you'd create instances of your ajax request like so:

     $('#formone, #formtwo, #formthree').ajaxForm({
       beforeSubmit: showLoader,
       success: hideLoader
     }); 
webfac
Alternatively, all forms could have the same CLASS, for example .myform and then you'd bind the ajax event to the class, like so:$('.myform').ajaxForm...
webfac
Thanx! This doesn't save my need to make live binding though (Nick's solution does however).
Jerry2
A: 

I'll try and address these one at a time to better match the question:

1) You can re-bind when you .load() (or whatever jQuery ajax method you're using) or use a plugin like livequery(), for example here's re-binding (do this in your success handler):

$("#myDynamicDiv .myForm").ajaxForm({ ...options... }); 

Or using livequery():

$(".myForm").livequery(function() { $(this).ajaxForm({ ...options... }); });

2) Use a class instead of IDs here, like this: class="myForm", whenever you want to handle batches of elements like this class is a pretty safe route. The examples above work with class and not IDs per form (they can have IDs, they're just not used). Your form tags would look like this:

<form class="myForm">

3) The same solutions in answer #1 account for this :)

Nick Craver
Hi there again and again thanx for your help... Right before your answer I tried one more thing: $('#myForm').live("submit", function(event) { $(this).ajaxSubmit({ .... and it WORKS. Thanx for the tip of not using ID's. It is wrong the form has same ID's although it works.I am just wondering what is the main difference on my live("submit") and your livequery?
Jerry2
@Jerry2 - You're just attaching the ajax form before the submit action finishes...this works but your `beforeSend` may be affected, as it would serialize data at the wrong time IIRC. I would definitely avoid multiple IDs though, they can cause other issues...you can stick with what you have just use classes and `$('.myForm').live()` :)
Nick Craver
So your solution of livequery is better to use in this situation?
Jerry2
@Jerry2 - It's a catch-all solution, the *optimal* way to do this is binding after your do your load in the `success` callback/handler. If you're unable, `.livequery()` is a decent fallback :)
Nick Craver
I can bind after I do the load, yes. So after each load I use bind for forms, not live?
Jerry2
What happens to the old forms that dissapear, are they automatically unbinded?
Jerry2
@Jerry2 - How are you loading the content? If you call `.empty()` before the `.load()` it'll unbind everything appropriately :)
Nick Craver
I have edited my post to show the whole loader. It loads the contents div with new data that has 3 forms and binds the AjaxForm to the new forms that are loaded. My problem is that the loader is never shown. I tried with alert before hide() and then it is shown. So I guess it is not shown because it doesn't wait till the form is submitted, but I properly used the sucess callback ;-(
Jerry2
Also first time I click on submit in form it doesn't work. From the second time it works :-(
Jerry2