jquery - thinkphp3.2.3's verification code automatically refreshes
迷茫
迷茫 2017-05-16 13:16:38
0
6
504

How to write automatic refresh of Ajax verification code error in thinkphp?
Write it here in else of this return function

Attach the following html


How to trigger this click time again where else?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(6)
黄舟

You can write the verification code yourself, or you can use a third-party library. There are many third-party libraries now. For the refresh mechanism, it is recommended to use the API method. Just write a Javascript to give a random number to distinguish.
Example:
Here I am using the third-party verification code library gregwar/captcha

<input type="text" name="captcha" class="form-control" style="width: 300px;">
          <a onclick="javascript:re_captcha();" ><img src="{{ URL('code/captcha') }}"  alt="验证码" title="刷新图片" width="100" height="40" id="code" border="0"></a>

<script>  
  function re_captcha() {
    $url = "{{ URL('kit/captcha') }}";
        $url = $url + "/" + Math.random();
        document.getElementById('code').src=$url;
  }
</script>

If you mistakenly refresh automatically, call the re_captcha method again.

Ty80

The principle of refreshing the verification code is actually very simple, just add a suffix?t=123123

曾经蜡笔没有小新
else{
   $('#img').src = 'url?rand='+Math.random(); 
}
左手右手慢动作

1. Refresh in the else part of the callback. The method is given above.

2. If you have done the first point and it has not been refreshed, then I guess there is something wrong with your judgment:
if(data.info==1)
Here, should it be: if(data.status== 1) ????

3.$("#2")What is it? The ID cannot start with a number.

PHPzhong

My personal habits are probably like this

  1. Save a src on the verification code image and save the original address of the verification code (to prevent the image address from getting longer)

  2. Bind click switching event

  3. When the verification code is wrong, the verification code click event is released through trigger to switch the verification code

//代码手写 难免有误
$(function(){
    var verifyImg = $("#verify_img");
    verifyImg.click(function(){
        $(this).attr("src",$(this).data('src') + '?v=' + Math.random());
    }).data('src', verifyImg.attr('src'));
    
    $("#fm-xxx").submit(function(){
        var fm = $(this);
        $.post(fm.attr('action'), fm.serialize(), function(data){
            if(data.code == 'verify-code') {
                verifyImg.trigger("click");
            } else {
                //...
            }
        });
        return false;
    });
});
仅有的幸福

Add below

$("#2").html(验证码错误);

$("#verify_img").attr("src",<?php echo U('Index/yzm');?>);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!