2013-01-04 73 views
7

不工作我实现了一个电子邮件验证码。当点击linkEmail按钮电子邮件模式将打开。 那里我必须设置一个处理程序(CaptchaGenerator.ashx)生成的验证码图像点击linkEmail按钮单击。这是代码。动态更改图片src使用jQuery在IE和Firefox

$(".linkEmail").click(function() { 
    //Load captcha image 
    $('.imgCaptcha').attr('src', '/Custom/AppCode/Utilities/CaptchaGenerator.ashx'); 
    $('#emailModal').modal(); 
}); 

上面的代码工作正常,在克罗姆但不是在IE和Firefox的工作。 虽然我试过以下,但没有运气。

HTML:

<p id="captchacontainerp" class="captchacontainer"></p> 
------------------------------------------------------------- 
$('#captchacontainerp').prepend($("<img id='imCaptcha' class='imgCaptcha' src='/Custom/AppCode/Utilities/CaptchaGenerator.ashx'></img>")); 
------------------------------------------------------------- 
var img = $('<img id="imCaptcha" class="imgCaptcha">'); 
img.attr('src', '/Custom/AppCode/Utilities/CaptchaGenerator.ashx'); 
$('#captchacontainerp').empty(); 
img.appendTo('#captchacontainerp'); 
--------------------------------------------------------------- 
$('#captchacontainerp').empty(); 
$('#captchacontainerp').append($("<img id='imCaptcha' class='imgCaptcha' src='/Custom/AppCode/Utilities/CaptchaGenerator.ashx'></img>")); 
+0

http://jsfiddle.net/ghwYF/为我工作得很好(打完跑后,或者你得到404的那个图像) – llamerr

+0

尝试ajax调用,因为我在我的答案中解决了你的问题.. –

回答

8

IE缓存所有的GET请求,所以时间戳添加到您请求的URL如:

$(".linkEmail").click(function() { 
    //Load captcha image 
    $('.imgCaptcha').attr('src', '/Custom/AppCode/Utilities/CaptchaGenerator.ashx?'+new Date().getTime()); 
    $('#emailModal').modal(); 
}); 
+0

是的!问题是缓存,如你所说,谢谢 –

+0

非常感谢。我尝试了一切,如 'Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetExpires(DateTime.Now); Response.Cache.SetNoServerCaching(); Response.Cache.SetNoStore();'不适合我。但是,做魔术 – Satheesh

+0

这个代码这个简单的代码工作完美... :) – yadavr

1

您是否尝试过再更改它之前设置src属性 '' ? 此外,什么是您所使用的缓存设置(包括本地和服务器上)

+1

改变'src'属性为“”将导致当前页面的请求,不这样做 – JamesHalsall

0

如果指定任何内嵌样式属性,高度或宽度一样,

<img src="themes/images/01.png" height="100" width="100" /> 
<img src="themes/images/01.png" style="height:100px; width=100px;" /> 

那么首先除去它然后再试一次。

即使你使用的样式标签外部指定样式,

#imageId{ 
    height : 100px; 
    width : 100px; 
    } 

然后还首次将其删除,并尝试。 从图像中删除样式属性后,它将显示图像。

高度属性是可以与IE工作,但宽度属性不工作。

如果以上方法无效,那么: 有时PNG文件不显示为好。所以尽量使用他们的JPG图片。

0

我试图调用重新验证码按钮时有同样的问题。 经过一番搜索,现在的功能,几乎所有著名的浏览器(Chrome浏览器,火狐,IE,边缘,...)工作正常:

function recaptcha(theUrl) { 
    $.get(theUrl, function(data, status){}); 
    document.getElementById("captcha-img").src = ""; 
    setTimeout(function(){ 
    document.getElementById("captcha-img").src = "captcha?"+new Date().getTime(); 
    }, 0); 
} 

“theUrl”是用来提供新的验证码图片,可以忽略在你的情况。最重要的一点是生成强制FF和IE重新渲染图像的新URL。