读书人

jquery遇到了个有关问题

发布时间: 2012-08-28 12:37:01 作者: rapoo

jquery遇到了个问题
自定义了一个验证码刷新函数,如下:

JScript code
    //验证码刷新函数    function captchaRefresh()     {        alert( "captchaRefresh" );        $( "#img-captcha" ).src = $( "#img-captcha" ).src +"?random="+ Math.random();        captchaIsTrue = false;                $( "#captcha" ).attr( "value", "" );        $( "#info" ).html( "请输入验证码" );    }


程序里在这两个地方调用了这段验证码刷新函数:
此处的调用标记为①
JScript code
    //验证码刷新事件    $( "#img-captcha" ).bind( "click", captchaRefresh );

此处的调用标记为②
JScript code
    //登陆验证-回调函数    function loginCallback( xml )     {        var output = $( xml ).find( "root" ).find( "result" ).text();        if( output == "登陆成功" )        {            location.href = "DrawCaptcha";        }        else        {            $( "#info" ).html( output );            captchaRefresh();        }    }

-------------------------------------------------------------------

①②都调用这段调用验证码刷新函数
都只弹出"captchaRefresh"的对话框,
$( "#img-captcha" ).src = $( "#img-captcha" ).src +"?random="+ Math.random();
这段图片刷新代码不起作用

修改这段代码为:
this.src = this.src +"?random="+ Math.random();
①处可以刷新,②处不能,因为②处调用的this没有给定。

要怎么改才能让两处都能刷新呢?
为什么
$( "#img-captcha" ).src = $( "#img-captcha" ).src +"?random="+ Math.random();
这样写不起作用呢?
本人初学,有很多不懂的地方…

[解决办法]
JScript code
// $( "#img-captcha" ).src = $( "#img-captcha" ).src +"?random="+ Math.random();$( "#img-captcha" ).attr('src', $( "#img-captcha" ).attr('src') +"?random="+ Math.random()); 

读书人网 >JavaScript

热点推荐