2013-03-09 26 views
3
$('.cn').click(function() { 
     var pic_id = $(this).attr('href'); 
     console.log(pic_id); 
     //alert(pic_id); 

    $.ajax({ 
      type: "POST", 
      url: "<?php echo base_url();?>anda/coins", 
      async: false, 
      data: "pic_id="+pic_id, 
      dataType: 'json', 
      success: function(data){ 
       //alert(data); 
       $('.cn_point').html(data.id); 
      } 
      }); 

     }); 

我接到電話回值並顯示在頁面上,但得到刷新和跨度隱藏的價值。有沒有人可以幫助我?我無法找到我的錯誤。錯誤的jQuery AJAX方法和頁面刷新獲得

+1

認沽'返回false;而'$ .ajax'功能後'權 – asprin 2013-03-09 06:32:51

+0

非常感謝。我阿司匹林解決我的問題...... – 2013-03-09 06:44:59

回答

5

嘗試使用preventDefault

$('.cn').click(function (e) { 
    e.preventDefault(); 
    var pic_id = $(this).attr('href'); 
    console.log(pic_id); 
    //alert(pic_id); 

    $.ajax({ 
     type : "POST", 
     url : "<?php echo base_url();?>anda/coins", 
     async : false, 
     data : "pic_id=" + pic_id, 
     dataType : 'json', 
     success : function (data) { 
      //alert(data); 
      $('.cn_point').html(data.id); 
     } 
    }); 

}); 
+0

感謝你們,你對我的熱心幫助.. – 2013-03-09 06:45:55

+0

@DebendraSamal展通過接受這個答案;點擊它旁邊的複選標記。 – Daedalus 2013-03-09 07:43:02

+0

我可以知道什麼是概述複選標記? – 2013-03-09 07:48:55

0

它,因爲你點擊鏈接,它會一直刷新頁面。

$('.cn').click(function() { 

    e.preventDefault(); 
    var pic_id = $(this).attr('href'); 
    console.log(pic_id); 
    //alert(pic_id); 

    $.ajax({ 
     type: "POST", 
     url: "<?php echo base_url();?>anda/coins", 
     async: false, 
     data: "pic_id="+pic_id, 
     dataType: 'json', 
     success: function(data){ 
      //alert(data); 
      $('.cn_point').html(data.id); 
     } 
     }); 

    }); 
+0

這不是問題,這不會解決頁面刷新問題 – asprin 2013-03-09 06:34:39

+0

您剛纔複製了我之前發佈的內容! XD – darshanags 2013-03-09 06:44:16

+0

我沒有複製你的答案。我回顧了我的答案,並注意到我的錯誤,因爲我沒有閱讀他問題的最後部分。你沒有告訴他爲什麼這樣做的原因。我只是解釋了爲什麼它刷新了他的頁面。所以你可以猜測,我沒有複製你的代碼。我修改了他的代碼。 – 2013-03-09 06:46:18