2017-08-11 65 views
0

我的刷新验证码按钮有问题,它只是在我重新加载页面后单击按钮时有效。我不知道我是错了还是错过了别的东西。刷新验证码图像jquery codeigniter

我尝试使用jquery刷新验证码,因为效率更高。

这是一块的我的代码:

控制器

public function refresh_login(){ 
    // Captcha configuration 
    $config = array(
     'img_path'  => 'captcha_images/', 
     'img_url'  => base_url().'captcha_images/', 
     'img_width'  => '100', 
     'img_height' => '30', 
     'word_length' => '4', 
     'font_size'  => '16' 
    ); 
    $captcha = create_captcha($config); 

    // Unset previous captcha and store new captcha word 
    $this->session->unset_userdata('captchaCode'); 
    $this->session->set_userdata('captchaCode',$captcha['word']); 

    // Display captcha image 
    echo $captcha['image']; 
} 

的jquery

$(document).ready(function() { 
    var base_url = '<?php echo base_url(); ?>'; 
    $('.reload-captcha').click(function(event){ 
     event.preventDefault(); 
     $.ajax({ 
      url:base_url+'index.php/auth/refresh_login', 
      success:function(data){ 
       $('.captcha-img').replaceWith(data); 
      } 
     });    
     }); 

视图

<form action="<?php echo base_url(); ?>index.php/auth/login/" id="login-form" method="post"> 
    <div class="form-group has-feedback"> 
    <input type="text" class="form-control" placeholder="Username" id="username" name="username" required> 
    <span class="glyphicon glyphicon-user form-control-feedback"></span> 
    </div> 
    <div class="form-group has-feedback"> 
    <input type="password" class="form-control" placeholder="Password" id="password" name="password" required> 
    <span class="glyphicon glyphicon-lock form-control-feedback"></span> 
    </div> 
    <div class="form-group"> 
    <p id="captImg" class="captcha-img"><?php echo $captchaImg; ?></p> 
    <input type="text" class="form-control" name="captcha" placeholder="Captcha" style="margin-bottom: 5px"/> 
    <a href="#" class="reload-captcha refreshCaptcha btn btn-info btn-sm" ><i class="glyphicon glyphicon-refresh"></i></a> 
    </div> 
    <div class="row"> 
    <!-- /.col --> 
    <div class="col-xs-8"> 
      <label><a href="#"><u>Forgot your password?</u></a></label> 
    </div> 
    <div class="col-xs-4 pull-right"> 
     <button type="submit" class="btn btn-primary btn-block btn-flat">Sign In</button> 
    </div> 
    <!-- /.col --> 
    </div> 
</form> 

回答

0

什么样的问题发生??? 尝试:

$('.captcha-img').attr("src", data); 

代替:

$('.captcha-img').replaceWith(data); 

,并添加

dataType: "text", 
    cache:false, 

如下:

$.ajax({ 
      url:base_url+'index.php/auth/refresh_login', 
      dataType: "text", 
      cache:false, 
      success:function(data){ 
       $('.captcha-img').attr("src", data); 
      } 
     });