Ajax forms in ListView only work on first page

I’m new at this, so it’s probably some stupid mistake, but…

This is the CListView’s view:




echo CHtml::beginForm('/controller/action','post',array('class'=>'ajax-form'));

	echo CHtml::hiddenField('asd',$data->asd);

	echo CHtml::submitButton('Submit');

echo CHtml::endForm();



This is the script I have included at the end of the body tag in the main layout file.




$(function() {

	$('.ajax-form').submit(function(){

		$.ajax({

			type: 'POST',

			url: $(this).attr('action'),

			data: $(this).children('input').serialize(),

			...

		});

		return false;

	});

});



Pagination works with ajax (without ajax pagination, everything is fine).

How come the forms only work on the first page? I mean, the class for each form always remains the same and the js file is always included no matter what.

Is a problem of client script.

Try doing the render partial with the fourth parameter setted to true, it will process output and adding clientscript.

Nope, nothing changed.

You need:




$(function() {

        $('.ajax-form').live('submit',function(){

                $.ajax({

                        type: 'POST',

                        url: $(this).attr('action'),

                        data: $(this).children('input').serialize(),

                        ...

                });

                return false;

        });

});



Thank you very much. I was ready to start pulling my hair out.

You welcome, just remember as long as the content you want to manipulate comes via an ajax call, then live() is the answer to your questions :)

Also, don’t over-use the live() event, use it only for this type of action, when the content is not initially into your page or it is changed by an ajax call .