怎样使用jquery刷新验证码图片
2017-03-08 18:38
316 查看
之所以普通的做法无法实现实时更新,是因为被浏览器缓存了,所以加了个时间戳
参考:http://blog.csdn.net/fengyu09/article/details/50393856
How
to refresh the src of <img /> with jQuery?
where test.PHP generates an image with a random number.
Itried :
But it doesn't work.
Add a timestamp or a random number:
怎样使用jQuery刷新验证码图片?
(使用ajax读取django返回的 image/gif,不可行。)
在点击图片时,在其Src属性上增加一个随机数,使用时间戳即可。
附代码:
[java] view
plain copy
/**
* 验证码,提交前验证
*/
$(document).ready(function(){
var validated = false;
$.ajaxSetup({
dataType: "json",
beforeSend: function(xhr){
var csrftoken = $.cookie('csrftoken');
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
});
// 验证码输入失去焦点
$("#id_code").blur(function() {
//alert(1);
$.post(
$("#id_url_check").val(),
{"code": $("#id_code").val()},
function (data) {
if (data.status == true)
validated = true;
}
);
});
// 提交表单
$("form").submit(function(event){
if (validated)
return true;
else {
$( "span" ).text( "验证码不正确。" ).show().fadeOut( 3000 );
// 更换验证码
var timestamp = new Date().getTime();
$("#id_img_captcha").attr("src", $("#id_url_captcha").val() + '?' +timestamp );
event.preventDefault();
}
}
);
// 点击更换验证码
$('#id_img_captcha').click(function() {
var timestamp = new Date().getTime();
$(this).attr("src", $("#id_url_captcha").val() + '?'+ timestamp);
});
//
});
参考:http://blog.csdn.net/fengyu09/article/details/50393856
How
to refresh the src of <img /> with jQuery?
<img src="test.php" />
where test.PHP generates an image with a random number.
Itried :
$('#verifyimage').click(function() { $(this).attr('src',$(this).attr('src')); });
But it doesn't work.
Add a timestamp or a random number:
var timestamp = new Date().getTime(); $(this).attr('src',$(this).attr('src') + '?' +timestamp );
怎样使用jQuery刷新验证码图片?
(使用ajax读取django返回的 image/gif,不可行。)
在点击图片时,在其Src属性上增加一个随机数,使用时间戳即可。
附代码:
[java] view
plain copy
/**
* 验证码,提交前验证
*/
$(document).ready(function(){
var validated = false;
$.ajaxSetup({
dataType: "json",
beforeSend: function(xhr){
var csrftoken = $.cookie('csrftoken');
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
});
// 验证码输入失去焦点
$("#id_code").blur(function() {
//alert(1);
$.post(
$("#id_url_check").val(),
{"code": $("#id_code").val()},
function (data) {
if (data.status == true)
validated = true;
}
);
});
// 提交表单
$("form").submit(function(event){
if (validated)
return true;
else {
$( "span" ).text( "验证码不正确。" ).show().fadeOut( 3000 );
// 更换验证码
var timestamp = new Date().getTime();
$("#id_img_captcha").attr("src", $("#id_url_captcha").val() + '?' +timestamp );
event.preventDefault();
}
}
);
// 点击更换验证码
$('#id_img_captcha').click(function() {
var timestamp = new Date().getTime();
$(this).attr("src", $("#id_url_captcha").val() + '?'+ timestamp);
});
//
});
相关文章推荐
- 怎样使用jquery刷新验证码图片
- 使用jquery无刷新上传图片,可用于头像预览功能
- js怎样实现图片的懒加载以及jquery.lazyload.js的使用
- 如何生成验证码图片,并且不刷新页面就可换图片
- FireFox,IE7点击图片刷新验证码bug的解决
- asp.net treeview控件无刷新选择和删除节点(使用jquery)
- Asp.net 2.0 TreeView控件使用jQuery无刷新添加节点详细说明
- 如何在用户注册或登录时使用验证码图片
- 关于怎样使用Picture Control显示BMP图片
- Asp.net 2.0 TreeView控件使用jQuery无刷新添加节点详细说明
- IE7,FireFox点击图片刷新验证码bug的解决
- 使用ASP生成任意长度彩色BMP验证码图片
- 使用python做验证码图片
- js实现点击验证码无刷新重新加载验证码图片
- 使用jQuery自动缩图片
- 如何在用户注册或登录时使用验证码图片(转)
- 怎样在vb.net中将图片存入SQL Server 2000并能读出来使用?
- 怎样在vb.net中将图片存入SQL Server 2000并能读出来使用?
- httphandler生成无刷新验证码图片
- Jquery实现无刷新验证码