dslf46995 2011-06-11 05:33
浏览 210

使用jQuery问题进行Captcha刷新

I tried researching on Google and I browsed through some question here. I tried some of the solutions but I can't get them to work.

<td>
    <img id="captcha" src="captcha.php" name="captcha" />
    <img src="refresh.jpg" width="25" alt="" id="refresh_captcha" name="refresh_captcha" />
    <input type="text" name="txtCaptcha" id="txtCaptcha" size="10" />
</td>

The code below doesn't work:

$('.refresh_captcha').click(function(){
    $('img').attr('src', 'captcha.php?cache=' + new Date().getTime());
});

I also tried this:

$("#refresh_captcha").click(function() {
    $("#captcha").attr("src","captcha.php?r=" + Math.random());
});

And this as well:

jQuery(function($) {
    jQuery('#captcha').after("<a href=\"#\" id=\"refresh_captcha\">Refresh<\/a>");
    jQuery('body').delegate('#refresh_captcha','click',function(){
        jQuery.ajax({
            'success':function(html){
                jQuery("#captcha").attr("src",html)
            },
            'url':'/captcha.php?refresh=1',
            'cache':false
        });
        return false;
    });
});

Anyone can help me?

  • 写回答

2条回答 默认 最新

  • donmqryh49993 2011-09-06 11:44
    关注

    Did you try to empty the cache as well? (EDIT: yes, you did...) Did the problem appear in all browser? I reckon that your browser caches the old image, because your direct call seems to work. Try cache: false instead of 'cache' = false

    For me this solution using ajax-calls and Java Spring in backend worked:

    function generateCaptcha() {
    
        $.ajaxSetup({
          cache: false
        });
    
        var timestamp = (new Date()).getTime();
    
        requestMappingCaptcha = "/generateCaptcha";
    
        jQuery.get(requestMappingCaptcha, timestamp, function(data) {
                    $("#captchaImg").slideUp("fast");
    
                    if (!$.browser.msie || ($.browser.msie && $.browser.version == "9.0")) { 
                            // animate reloadArrows
                            $("#reloadArrows").rotate({
                            angle:0, 
                            animateTo:360
                        });  
                    }
    
                    // setting new source
                    var newSrc = $("#captchaImg").attr("src").split("?");
                    newSrc = newSrc[0] + "?" + timestamp;
                    $("#captchaImg").attr("src", newSrc);
                    $("#captchaImg").slideDown("fast");
            });
    }
    

    And the HTML for that:

                            <div class="container captcha">
                                <spring:url value="/captcha.jpeg" var="url"/>
                                <div class="step2_captcha">
                                    <img id="captchaImg" src="${url}" alt="Generatin captcha..." width="180" height="42" border="0" name="captchaImg"/>
                                </div>
                                <span class="help captcha">
                                    <img src="/r.png" onclick="generateCaptcha();" id="reloadArrows" title="Reload Captcha"/>
                                </span>
                                &nbsp;
                            </div>
    

    What does it do!? By clicking the icon the function generateCaptcha will be called. The request mapping called a server function (in my case java) and renders a new captcha to the same URL the old one was. Its importent to disable caching and send a timestamp with the get. After doing that a little jQuery magic is done, the source of the captcha gonna changed to the image-Url + timestamp.

    Hope it'll work for you php as well.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大