使用jQuery验证码刷新问题

发布于 11-15 02:37 字数 1136 浏览 1 评论 0原文

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

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

有人可以帮助我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久光2024-11-22 02:37:21

您是否也尝试清空缓存? (编辑:是的,你做了...)问题是否出现在所有浏览器中?我认为您的浏览器缓存了旧图像,因为您的直接调用似乎有效。尝试用 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>
                             
                        </div>

它有什么作用!?通过单击该图标,将调用函数generateCaptcha。请求映射调用服务器函数(在我的例子中是 java),并将新的验证码呈现到与旧验证码相同的 URL。禁用缓存并使用 get 发送时间戳非常重要。在完成了一些 jQuery 魔法之后,验证码的来源将更改为图像 Url + 时间戳。

希望它也适用于你的 php。

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

如果没结果2024-11-22 02:37:21
        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('');
        }

最良好的问候!

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

best regard!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文