views:

93

answers:

4

Hi,

I am trying to make a script that updates the captcha image, that is loaded through live() function... It works, but it only updates the image 1 time on firefox, 2 times on safari... How can I make this to work multiple times?

jquery 1.4.2

relevant part of code:

/* captcha image change */
var rand = Math.random();

$('a.captcha_refresh').live('click', function() {
    $('img.captcha').attr("src", 'captchashow.php?sid=' + rand);
}); 

thanks, brm

+6  A: 

This depends on your implementation, but you are reusing the same random value on all request. You may want:

var rand;

$('a.captcha_refresh').live('click', function() {
    rand = Math.random(); //new value
    $('img.captcha').attr("src", 'captchashow.php?sid=' + rand);
}); 

That way rand keeps changing, but you see its latest value.

Kobi
+2  A: 

move

var rand = Math.random();

inside function();

falkon
A: 

Hi,

thanks for both answers, seems obvious, it is fine now... :/

One more question on the same subject... On first click I chnage the button class and name, on second I post to external script and change button back to first value. On second post the php script is executed ut without any data passed...

/* change client IPv4 address */
$('input.submit').live('click', function() {
    /* get current ip value */
    var ipv4 = $('td.user_ipv4').html();

    /* change submit button value and class */
    $('td.changeipv4').html('<input type="button" class="submit_ipv4" value="Spremeni">');

    /* user input */
    $('td.user_ipv4').html('<input type="text" size="15" maxlength="15" value="' + ipv4 + '">');

    /* change IP! */
    $('input.submit_ipv4').live('click', function() {
        /* get submitted IP address value */
        var ipv4 = $('td.user_ipv4 input').val();

        $.post('change_ipv4.php', { ipv4: ipv4 } , function(data) {
            $('td.changeipv4_result').html(data);
        });

        /* back to old change button */
        $('td.changeipv4').html('<input type="button" class="submit" value="Uredi">');
            /* print IP address */
            $('td.user_ipv4').html(ipv4);
    });
});
A: 

figured it out, the second submit must be separate...

/* change client IPv4 address */
$('input.submit').live('click', function() {
    /* get current ip value */
    var ipv4 = $('td.user_ipv4').html();

    /* change submit button value and class */
    $('td.changeipv4').html('<input type="button" class="submit_ipv4" value="Spremeni">');

    /* user input */
    $('td.user_ipv4').html('<input type="text" size="15" maxlength="15" value="' + ipv4 + '">');

});
    /* change IP! */
    $('input.submit_ipv4').live('click', function() {
        /* get submitted IP address value */
        var ipv4 = $('td.user_ipv4 input').val();

        $.post('change_ipv4.php', { ipv4: ipv4 } , function(data) {
            $('td.changeipv4_result').html(data);
        });

        /* back to old change button */
        $('td.changeipv4').html('<input type="button" class="submit" value="Uredi">');
            /* print IP address */
            $('td.user_ipv4').html(ipv4);
    });