2013-03-03 15 views
0

這是鏈接重裝外頁onLinkClick通過AJAX和jQuery

<a href="javascript:loadCaptcha();">Reload</a> 

我想點擊它,並通過AJAX和jQuery加載外部文件。在我的頁面標題中,我有以下JavaScript代碼。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script language="javascript" type="text/javascript"> 
    $(function(){ 
     loadCaptcha(); 
    }); 
    function loadCaptcha() 
    { 
     $.ajax({ 
      type: 'POST', 
      url: 'captcha.php', 
      data: "", 
      cache: false, 
      success: function(res){ 
       $('#captcha').html(res); 
      } 
     }); 
    } 
</script> 

我想重新加載captcha.php點擊該鏈接。在頁面加載文件captcha.php加載ID #captcha - 但在該鏈接點擊它不重新加載。

captcha.php代碼

<?php 
$capt = generate_captcha(); 
echo '<img src="captcha.jpg" id="imgcaptcha" name="imgcaptcha" alt="'.$capt.'" style="max-width: 110px; max-height: 35px;" title="Captcha"/><input type="hidden" name="captchacode" id="captchacode" value="'.$capt.'" />'; 

function generate_captcha() 
    { 
     $number= rand(11111, 99999); 
     $width = 110; 
     $height= 35; 
     //create image using imagecreate php function 
     $img = imagecreate($width, $height); 
      $backcolor = imagecolorallocatealpha(
         $img, 
         hexdec(substr('FFFFFF', 0, 2)), 
         hexdec(substr('FFFFFF', 2, 2)), 
         hexdec(substr('FFFFFF', 4, 2)), 
         127 * (100 - 100)/100 
        ); 
     imagefill($img, 0, 0, $backcolor); 
      //create line color 
     $linescolor = imagecolorallocatealpha(
         $img, 
         hexdec(substr('333333', 0, 2)), 
         hexdec(substr('333333', 2, 2)), 
         hexdec(substr('333333', 4, 2)), 
         127 * (100 - 70)/100 
        ); 
     //generate different lines 
     for($i=0; $i<10; $i++) { 
      imageline($img, mt_rand(0,110), mt_rand(0,35), 
      mt_rand(0,110), mt_rand(0,35), $linescolor); 
      } 
     //Font file path 
     $font_path = "jokerman.ttf"; 
     //create color for font 
     $font_color = imagecolorallocatealpha(
         $img, 
         hexdec(substr('000000', 0, 2)), 
         hexdec(substr('000000', 2, 2)), 
         hexdec(substr('000000', 4, 2)), 
         127 * (100 - 100)/100 
        ); 
     //add characters to the image 
     imagettftext($img, 22, 0, 5, 28, $font_color, $font_path, $number); 
     //store image 
     imagejpeg($img, "captcha.jpg", 100); 
     return $number; 
    } 
?> 

UPDATE:鉻是示出更新後的圖像,Firefox和IE沒有更新圖像。緩存問題。我沒有通過PHP頭緩存,但沒有運氣。

請指教。而不是href="javascript:loadCaptcha();"

<a onclick="loadCaptcha();">Reload</a> 

OR

<a id="reloadCaptcha">Reload</a> 

<script type="text/javascript"> 
$(document).on("click", "#reloadCaptcha", loadCaptcha); 
</script> 
+0

我認爲你的問題在別處... – yckart 2013-03-03 04:16:20

+0

是的,我相信是的。我能夠加載文件,但我無法通過使用相同功能的鏈接重新加載它。 – 2013-03-03 04:16:45

+0

您能否向我們提供您的captcha.php中的代碼? – yckart 2013-03-03 04:22:24

回答

0

使用onclick IE和Firefox不會刷新驗證碼圖片,由於緩存問題。解決辦法是每次刪除驗證碼圖像並創建具有唯一名稱的新驗證碼圖像。因爲名稱將是唯一的,所以驗證碼將被刷新,FF將重新加載圖像,因爲它的新增功能。

我已經解決了這個問題,每次重新創建獨特的驗證碼圖像並在重新載入時調用該獨特的圖像。

+0

仍然無法正常工作。 – 2013-03-03 04:06:59

+0

嘗試通過jquery綁定點擊事件 – Derek 2013-03-03 04:12:14

+0

嘗試這一個,現在...仍然沒有重新加載。 – 2013-03-03 04:16:25

0