我正在使用jquery和codeigniter创建catcha。我的问题是在刷新验证码时,src没有刷新。
以下是创建验证码的代码
<?php
类Get_captcha扩展了Controller {
function Get_captcha()
{
parent::controller();
$this->load->library('session');
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->model('tools_model');
}
function index($random = false)
{
$rand_string = $this->tools_model->RandomString(5);
$new_sess_data = array("random_number"=>$rand_string);
$this->session->set_userdata($new_sess_data);
$image = imagecreatetruecolor(148, 25);
$dir = 'fonts/';
$num = rand(1,2);
if($num==1)
{
$font = "Capture it 2.ttf"; // font style
}
else
{
$font = "Molot.otf";// font style
}
// random number 1 or 2
$num2 = rand(1,2);
if($num2==1)
{
$color = imagecolorallocate($image, 113, 193, 217);// color
}
else
{
$color = imagecolorallocate($image, 163, 197, 82);// color
}
$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 18, 0, 38, 22, $color, $dir.$font, $this->session->userdata('random_number'));
header("Content-type: image/png");
imagepng($image);
}
}
html的代码
<img src="<?php echo site_url('get_captcha') ?>" alt="" id="captcha" /><a href="#" class="b" id="refresh-captcha">Refresh</a>
jquery的代码
$('#refresh-captcha').click(function(){
$('#captcha').attr('src','<?php echo site_url('get_captcha') ?>');
});
答案 0 :(得分:0)
看起来你实际上并没有改变图像。您需要更改浏览器的URL以再次查找图像。尝试将JS代码更改为:
$('#refresh-captcha').click(function(){
var random = new Date().getTime();
$('#captcha').attr('src','<?php echo site_url('get_captcha') ?>?random='+random);
});
这将迫使图像“不同”#34;每次按下按钮。