2010-06-01 99 views
2

我正在嘗試製作一個腳本,用於更新驗證碼圖像,該圖像是通過live()函數加載的...它可以工作,但它只會更新圖像1次在firefox,2在safari上的時間...我如何使這個工作多次?jquery .live()點擊多次

的jQuery 1.4.2代碼

相關部分:

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

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

感謝, BRM

+0

作爲一個側面說明,你可能需要使用'委託()',而不是'活()'。速度有點快。 – 2010-06-01 12:45:52

回答

6

這取決於具體的實現,但要重複使用的所有要求相同的隨機值。你可能想:

var rand; 

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

這樣rand不斷變化,但你看它的最新值。

2

移動

var rand = Math.random(); 

內部函數();

0

想通了,第二提交必須分開......

/* 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); 
    });