我试着在谷歌上搜索,在这里浏览了一些问题。我尝试了一些解决方案,但无法使它们发挥作用。
<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;
});
});
有人能帮我吗?
您是否也尝试清空缓存?(编辑:是的,你出现了…)所有浏览器都出现了问题吗?我估计你的浏览器会缓存旧图像,因为你的直接调用似乎有效。尝试cache: false
而不是'cache' = false
对我来说,这个在后台使用ajax调用和JavaSpring的解决方案奏效了:
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),并将一个新的captcha呈现到旧的URL。重要的是禁用缓存并用get
发送时间戳。在做了一些jQuery魔术之后,captcha的源将更改为图像Url+时间戳。
希望它也能为你的php工作。
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('');
}
致以最崇高的敬意!