2014-04-07 109 views
1

我有一個負載報告。每個報告都是自己的頁面。當用戶在一個頁面上時,我希望他們能夠將報告添加到他們的收藏夾。我不是指瀏覽器的最愛,我的意思是他們最喜歡的報告,當他們登錄到系統的jQuery - 添加到收藏夾

谷歌搜索這個,由於顯而易見的原因,帶回無盡的教程/腳本如何添加到瀏覽器的收藏夾,這不是我要

我有一個按鈕,說:「添加到收藏夾」。點擊時,應該添加報告。然後,按鈕應該被移除並用一個說法改爲「從收藏夾中刪除」

我知道ASP/PHP將需要做實際的添加/刪除,但如何實現,這將是最有幫助的任何指導。

難道是沿

$(function() { 
    $('.report_add').click(function() { 
    this_id= this.attr("id"); 
    $.ajax({ 
     type: 'POST', 
     data: 'reportid'+this_id+'=&userid=789', 
     success: function() { ... }, 
     error: function(){ ... }, 
     url: '/url/', 
     cache:false 
    }); 
    }); 
}); 
+0

我不知道這是什麼問題的目的是,因爲沒有任何服務器端代碼調用您的AJAX是沒有意義的。 –

+0

使用$(這).attr(「ID」),而不是this.attr(「ID」),以確保jQuery的正常工作 –

+0

@RoryMcCrossan - 我知道一旦數據已發佈到PHP/ASP – pee2pee

回答

2

線的東西可以改變這一點:

this_id= this.attr("id"); 

data: 'reportid'+this_id+'=&userid=789', 

這樣:

var this_id= this.id; 

data: 'reportid='+this_id+'&userid=789', 

或:

var this_id= $(this).attr("id"); // add a jQuery wrapper 

data: 'reportid='+this_id+'&userid=789', 

在你的代碼有兩個問題

1.你是不是正確採摘ID,因爲您應用jQuery的.attr()方法DOM節點,而不是jQuery對象。使,必須爲this.id$(this).attr('id')

2.您的數據串不能很好地形成:

data: 'reportid'+this_id+'=&userid=789', 
//-------------^----------^--------------your '=' sign is at wrong place 

相反,你可以發送值是這樣的:

data: 'reportid='+this_id+'&userid=789', 

data: {reportid : this_id, userid : 789}, 

在你的代碼:

$(function() { 
    $('.report_add').click(function() { 
    var this_id= this.id; // <---------update this 
    $.ajax({ 
     type: 'POST', 
     data: {reportid : this_id, userid : 789}, // <---- and this 
     success: function() { ... }, 
     error: function(){ ... }, 
     url: '/url/', 
     cache:false 
    }); 
    }); 
}); 
+0

是做什麼的var必需? –

+0

@ C-鏈接我想是因爲似乎是一個gloabal變種可能產生問題。 – Jai

1

試試這個

$(function() { 
    $('.report_add').click(function() { 
    this_id= $(this).attr("id"); 
    $.ajax({ 
     type: 'POST', 
     data: 'reportid'+this_id+'&userid=789', 
     success: function() { 
     $(this).text('Remove From Favourite'); 
     }, 
     error: function(){ ... }, 
     url: '/url/', 
     cache:false 
    }); 
    }); 
});