2013-03-14 249 views
0

我有一個包含許多名爲.comment_holder的div的頁面,每個div都包含一個不同的ID號碼,例如,在.load()中傳遞一個變量

<div class="comment_holder" id="123"></div> 
<div class="comment_holder" id="345"></div> 

我想ID傳遞到一個網頁,其中我加載時執行操作時,其追加.comment_holder股利。這裏就是我想:

$('.comment_holder').load('/pages/includes/wall.php', { 
    var wlid = $(this).attr("id"); 
    wl_id: wlid; 
}); 

,但我得到:SyntaxError: missing : after property id

我應該怎麼做不同?

+0

你能不能驗證'this'引用了什麼對象? – Yoeri 2013-03-14 09:42:23

回答

3

data參數不是一個適當的對象字面量。 this也不在對象字面值範圍內,因此遍歷調用每個元素的加載函數的元素。使用傳遞給該函數的元素來檢索id。

$('.comment_holder').each(function(index, elem){ 
    $(elem).load('/pages/includes/wall.php', { 
     wl_id: $(elem).attr("id") 
    }); 
}); 

類似的例子:http://jsfiddle.net/qQ4Np/1/

-1

你不能有一個對象內部VAR語句字面那樣。所以剛ID分配給對象屬性

$('.comment_holder').load('/pages/includes/wall.php', { 
    wl_id: $("comment_holder").attr("id") 
}); 

但這是行不通的,如果你有多個元素白衣.comment_holder類。所以我建議使用Kevin的方法並循環收集。

+0

這並不能解決問題,'this'不會引用選定的元素。 – undefined 2013-03-14 09:48:04

+0

是真的。更新 – VeXii 2013-03-14 09:49:11

0
$('.clicker').click(function(){ 
var myId = $(this).attr("id"); 

$('.comment_holder').load('/pages/includes/wall.php', { 
    myId: myId; 
});}); 

而且wall.php:

$myId = $_POST['myId']; 
0

或者,如果要加載dynamiccaly在頁面加載數據,你必須這樣做,在環$。每()

$('.comment_holder').each(function(){ 
    var myId = $(this).attr("id"); 
    $(this).load('/pages/includes/wall.php', { 
     myId: myId; 
    }); 
});