繁体   English   中英

验证码刷新使用 jQuery 问题

[英]Captcha refresh using jQuery problem

我尝试在 Google 上进行研究,并在这里浏览了一些问题。 我尝试了一些解决方案,但我无法让它们工作。

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

下面的代码不起作用:

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

我也试过这个:

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

这也是:

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;
    });
});

任何人都可以帮助我吗?

        function fcapCha() {
            var timestamp = (new Date()).getTime();
            var newSrc = $(".capChaCaptchaImgID").attr("src").split("?");
            newSrc = newSrc[0] + "?" + timestamp;
            $(".capChaCaptchaImgID").attr("src", newSrc);
            $(".capChaCaptchaImgID").slideDown("fast");
        }
        function resetCapcha() {
            fcapCha();
            $('.acapChapinputCaptcha').val('');
        }

最良好的问候!

您是否也尝试清空缓存? (编辑:是的,你做到了......)问题是否出现在所有浏览器中? 我认为您的浏览器缓存了旧图像,因为您的直接调用似乎有效。 尝试使用cache: false而不是'cache' = false

对我来说,在后端使用 ajax 调用和 Java Spring 的解决方案有效:

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");
        });
}

HTML 为此:

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

它有什么作用?? 通过单击该图标,将调用 function generateCaptcha 请求映射称为服务器 function(在我的情况下为 java),并将新的验证码呈现为与旧验证码相同的 URL。 禁用缓存并使用get发送时间戳很重要。 在完成了一点 jQuery 魔术之后,验证码的来源将更改为图像-Url + 时间戳。

希望它也适用于您 php 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM