2013-03-21 63 views
-1

我有一個ajax刪除功能,但變量qid不顯示它在url中的值。它只是顯示它的名字。jquery顯示變量名稱而不是值

代碼:

$(function(){ 
    $(".delete_question").click(function(){ 
     var qid = 3; 
     $.ajax({ 
      type: "GET", 
      url: "<?php echo base_url(); ?>index.php/backend/questions/delete/"+qid, 
      data: '', 
      success: function(data){ 
       load_questions(); 
      } 
     }); 
     return false; //stop the actual form post !important! 
    }); 
}); 

什麼我的瀏覽器顯示:

url: "http://localhost/cit/index.php/backend/questions/delete/"+qid, 
+2

這是正確的,在JavaScript中的字符串使用時,+運算符可以作爲一個串連,所以當該值被評爲URL字符串將與QID變量的值串接,導致類似「http://localhost/cit/index.php/backend/questions/delete/3」 – cernunnos 2013-03-21 15:09:51

+1

你是說你看到'url:「http://localhost/cit/index.php/backend/questions/delete /「+ qid,'當你查看源代碼時,或者當執行ajax請求時,你的代碼中的'http://localhost/cit/index.php/backend/questions/delete/」+ qid'是否發送到服務器? – j08691 2013-03-21 15:14:56

+0

您對「我的瀏覽器顯示的內容」有什麼意思?代碼?開發者工具?網絡標籤? – zeroflagL 2013-03-21 15:15:03

回答

5

這似乎沒什麼問題。你實際上應該提出一個請求,並且看到它去了正確的地方。

+0

現在我覺得很蠢。謝謝您的回答 – 9edge 2013-03-21 15:16:47

0

qid是一個Javascript變量,所以不應該在JavaScript代碼中換出。

它重定向到的網址,將http://localhost/cit/index.php/backend/questions/delete/3

如果你希望它在JavaScript顯示爲http://localhost/cit/index.php/backend/questions/delete/3,你必須讓這個PHP。

像這樣:

$(function(){ 
    $(".delete_question").click(function(){ 
     <?php $qid = 3; ?> 
     $.ajax({ 
      type: "GET", 
      url: "<?php echo base_url(); ?>index.php/backend/questions/delete/<?php echo $qid; ?>", 
      data: '', 
      success: function(data){ 
       load_questions(); 
      } 
     }); 
     return false; //stop the actual form post !important! 
    }); 
});