2010-06-01 57 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); 
    });