2011-11-26 4 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

을 사용하지 말고 data을 입력하면 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 항상 문자열을 반환합니다.