刷新验证码图像

时间:2013-09-06 11:53:26

标签: php javascript

如何获取验证码图像的图像源以刷新验证码?

我试图这样做,但它不起作用:

 <img id="captcha_img" src="<?php echo  CAPTCHA_PLUGIN_URL . "/captcha_images.php?width=120&height=40&code=$code?>"?>" />


Can't read the image? click <a href='javascript: refresh_captcha();'>here</a> to refresh

<script type="text/javascript">
var refr_no = 0;
function refresh_captcha()
{
   var im = new Image();
   refr_no = refr_no + 1;
   im.src = "<?php echo  CAPTCHA_PLUGIN_URL . "/captcha_images.php?width=120&height=40"?>&refresh=" + refr_no;
   document.getElementById ("captcha_img").src = im.src;
}
</script>

2 个答案:

答案 0 :(得分:1)

您无需创建图像对象。

function refresh_captcha()
{
   im = document.getElementById('captcha_img');
   refr_no = refr_no + 1;
   im.src = "<?php echo  CAPTCHA_PLUGIN_URL . "/captcha_images.php?width=120&height=40"?>&refresh=" + refr_no;

}

查看此演示:http://jsbin.com/ayenOce/2/edit

答案 1 :(得分:0)

尝试:

<script type="text/javascript">
var refr_no = 0;
function refresh_captcha()
{
   refr_no = refr_no + 1;
   var oldSrc = document.getElementById("captcha_img").replace('&refresh='+(refr_no-1)+'','')+"&refresh="+refr_no;
   document.getElementById ("captcha_img").src = oldSrc;
   refr_no++;
}
</script>

<img id="captcha_img" src="<?php echo  CAPTCHA_PLUGIN_URL . "/captcha_images.php?width=120&height=40&code=$code?>"?>" />


Can't read the image? click <a href='#' onclick="refresh_captcha(); return false;">here</a> to refresh