July 24, 2010

jQuery, Ajax problem?

Question by user303832

I have one table, and when I click on row, it loads some content in another table.

The problem is, when I click on row the first time it loads just one time (message ‘Some msg’ and message ‘Some other msg’ is shown one time). When I click on another row, it loads twice (messages is shown twice). The third time when I click on row it loads three times, etc.

Here is my code:

$("#myTable tr").click(function(e){
    $.ajax({
        url:'<?php echo $full_path_ajax_php; ?>',
        data:{'what':'2','mypath':'12345678'},
        dataType:'json',
        type: 'GET',
        beforeSend:function(){alert('Some msg')},
        success: function(){alert('Some other msg')}
    });
    return false;
});

Answer by Starx

You need to use the .live() function in the parent table but don’t use this code in the portion which you are going to use:

$("#myTable tr").live('click', function(e){
    $.ajax({
        url:'<?php echo $full_path_ajax_php; ?>',
        data:{'what':'2','mypath':'12345678'},
        dataType:'json',
        type: 'GET',
        beforeSend:function(){alert('Some msg')},
        success: function(){alert('Some other msg')}
    });
    return false;
});

Remember, only once in the parent page or table.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!