怎样使用jquery刷新验证码图片

    xiaoxiao2021-03-25  154

    之所以普通的做法无法实现实时更新,是因为被浏览器缓存了,所以加了个时间戳

    参考: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);   });   //   });  
    转载请注明原文地址: https://ju.6miu.com/read-5389.html

    最新回复(0)