单击后jQuery无法识别某些属性

时间:2011-11-26 21:51:49

标签: jquery html

我有这个HTML:

<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 个答案:

答案 0 :(得分:2)

在我看来,您不应该使用attr而是data来获取data-属性。

答案 1 :(得分:0)

您需要使用以下其中一项:

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

注意,attr将始终返回一个字符串,而数据将尝试转换另一种类型,在本例中为数字。