之所以普通的做法无法实现实时更新,是因为被浏览器缓存了,所以加了个时间戳
参考:http://blog.youkuaiyun.com/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属性上增加一个随机数,使用时间戳即可。
附代码:
-
-
-
- $(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() {
-
- $.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);
- });
-
- });