1

如何获取验证码图片的图片源来刷新验证码?

我正在尝试这样做,但它不起作用:

 <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>
4

2 回答 2

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

于 2013-09-06T12:04:50.810 回答
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
于 2013-09-06T11:56:23.597 回答