2011-11-26 28 views
0

我有這樣的HTML:jQuery的不是點擊後認識的一些屬性

<div class="problem_comment_text" id="suggested_solution_text_110" ><p style="font-family: arial;">Solution name: <a class="expand_suggested_solution" href="#" data-suggestion_id="110" data-problem_id="228">Site to get peoples memories and experiences of every past game</a></p><p style="font-family: arial;">A site that would have a listing of every game for a particular team, and for each game, the fans who saw that game could share their memories of it. That would give a very holistic view of the game and people would discover much more about their team and the fans of those teams.</p><p style="color: #B77F37;">on Monday, 11-21-2011</p><div style="float:right; width: 250px;"><a class="delete_suggested_solution" data-problem_id="228" data-suggested_solution_id="110" href="#">Delete</a> | <a href="#" class="edit_suggested_solution" data-problem_id="228" data-suggested_solution_id="110" data-solution_text="A site that would have a listing of every game for a particular team, and for each game, the fans who saw that game could share their memories of it. That would give a very holistic view of the game and people would discover much more about their team and the fans of those teams." data-solution_title="Site to get peoples memories and experiences of every past game">Edit</a></div></div> 

,我有這樣的jQuery:

$('.edit_suggested_solution').live('click' , function() { 
    // Showing the wait image 
    $("#loading").show(); 

    var problem_id = $(this).attr("data-problem_id"); 
    var suggested_solution_id = $(this).attr("suggested_solution_id"); 
    var solution_title = $(this).attr("solution_title");   
    var solution_text = $(this).attr("solution_text"); 

    var dataString = 'problem_id='+ problem_id + '&suggested_solution_id=' + suggested_solution_id + '&solution_title=' + solution_title + '&solution_text=' + solution_text; 

    alert ("data string: " + dataString); 
}); 

的問題是,當用戶按下編輯鏈接,出來的我試圖得到的4個變量,我只能得到problem_id,其餘的都是未定義的。

有沒有人注意到代碼的任何特定問題,以瞭解爲什麼其他變量未定義?

回答

2

在我看來,你不應該使用attrdata得到data-屬性。

+0

良好的通話 - 做到了! – GeekedOut

+0

而你的HTML已經有問題了。屬性名稱是:'data-problem_id'和'data-suggested_solution_id',但是在您的js代碼中,您試圖通過'suggested_solution_id'獲得第二個屬性。 – melihcelik

0

您需要使用下列之一:

$(this).attr("data-suggested_solution_id"); 
$(this).data("suggested_solution_id"); 

注意,ATTR總是會返回一個字符串,而數據將嘗試轉換另一種類型,在這種情況下的數字。