0

这是链接

<a href="javascript:loadCaptcha();">Reload</a>

我想单击它并通过 ajax 和 jquery 加载外部文件。在我的页眉中,我有以下 javascript 代码。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
    $(function(){
        loadCaptcha();
    });
    function loadCaptcha()
    {
        $.ajax({
            type: 'POST',
            url: 'captcha.php',
            data: "",
            cache: false,
            success: function(res){
                $('#captcha').html(res);
            }
        });
    }
</script>

我想在单击该链接时重新加载 captcha.php。Onpageload 文件 captcha.php 正在加载 id #captcha - 但在该链接上单击它不重新加载。

验证码.php 代码

<?php
$capt = generate_captcha();
echo '<img src="captcha.jpg" id="imgcaptcha" name="imgcaptcha" alt="'.$capt.'" style="max-width: 110px; max-height: 35px;" title="Captcha"/><input type="hidden" name="captchacode" id="captchacode" value="'.$capt.'" />';

function generate_captcha()
    {
        $number= rand(11111, 99999);
        $width = 110;
        $height= 35;
        //create image using imagecreate php function
        $img = imagecreate($width, $height);
            $backcolor = imagecolorallocatealpha(
                      $img,
                      hexdec( substr( 'FFFFFF', 0, 2 ) ),
                      hexdec( substr( 'FFFFFF', 2, 2 ) ),
                      hexdec( substr( 'FFFFFF', 4, 2 ) ),
                      127 * ( 100 - 100 ) / 100
                    );
        imagefill($img, 0, 0, $backcolor);
            //create line color
        $linescolor = imagecolorallocatealpha(
                      $img,
                      hexdec( substr( '333333', 0, 2 ) ),
                      hexdec( substr( '333333', 2, 2 ) ),
                      hexdec( substr( '333333', 4, 2 ) ),
                      127 * ( 100 - 70 ) / 100
                    );
        //generate different lines  
        for( $i=0; $i<10; $i++ ) {
            imageline($img, mt_rand(0,110), mt_rand(0,35),
             mt_rand(0,110), mt_rand(0,35), $linescolor);
            }
        //Font file path
        $font_path = "jokerman.ttf";
        //create color for font
        $font_color = imagecolorallocatealpha(
                      $img,
                      hexdec( substr( '000000', 0, 2 ) ),
                      hexdec( substr( '000000', 2, 2 ) ),
                      hexdec( substr( '000000', 4, 2 ) ),
                      127 * ( 100 - 100 ) / 100
                    );
        //add characters to the image
        imagettftext($img, 22, 0, 5, 28, $font_color, $font_path, $number);
        //store image
        imagejpeg($img, "captcha.jpg", 100);
        return $number;
    }
?>

更新:chrome 正在显示更新的图像,firefox 和 ie 没有更新图像。缓存问题。我没有通过 php 标头使用缓存,但没有运气。

请指教。

4

2 回答 2

0

由于缓存问题,IE 和 Firefox 不会刷新验证码图像。解决方法是每次删除验证码图像并创建具有唯一名称的新验证码图像。因为名称是唯一的,所以验证码将被刷新,FF 将重新加载图像,因为它是新的。

我已经解决了这个问题,每次都重新创建唯一的验证码图像并在重新加载时调用该唯一的图像。

于 2013-03-03T12:48:37.120 回答
0

使用onclick代替href="javascript:loadCaptcha();"

<a onclick="loadCaptcha();">Reload</a>

或者

<a id="reloadCaptcha">Reload</a>

<script type="text/javascript">
 $(document).on("click", "#reloadCaptcha", loadCaptcha);
</script>
于 2013-03-03T04:02:13.107 回答