[英]Captcha refresh using jQuery problem
I tried researching on Google and I browsed through some question here.我尝试在 Google 上进行研究,并在这里浏览了一些问题。 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?任何人都可以帮助我吗?
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!最良好的问候!
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
尝试使用cache: false
而不是'cache' = false
For me this solution using ajax-calls and Java Spring in backend worked:对我来说,在后端使用 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");
});
}
And the HTML for that: 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>
What does it do??它有什么作用?? By clicking the icon the function generateCaptcha
will be called.通过单击该图标,将调用 function generateCaptcha
。 The request mapping called a server function (in my case java) and renders a new captcha to the same URL the old one was.请求映射称为服务器 function(在我的情况下为 java),并将新的验证码呈现为与旧验证码相同的 URL。 Its importent to disable caching and send a timestamp with the get
.禁用缓存并使用get
发送时间戳很重要。 After doing that a little jQuery magic is done, the source of the captcha gonna changed to the image-Url + timestamp.在完成了一点 jQuery 魔术之后,验证码的来源将更改为图像-Url + 时间戳。
Hope it'll work for you php as well.希望它也适用于您 php 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.