獲得通過AJAX調用元素的值我有一些jQuery代碼是這樣的:不能使用jQuery的
// Called right away after someone clicks on the vote up link
$('.vote_up').click(function()
{
var problem_id = $(this).attr("data-problem_id");
var class_name = ".votes_"+problem_id;
alert ("class name: " + class_name);
var x = $(".votes_214").val();
alert ("x: " + x); // returns nothing at first, but correct values later.
vote(problem_id , 1);
//Return false to prevent page navigation
return false;
});
,這裏是表決功能:
// Global function
var vote = function(problem_id , vote)
{
var dataString = 'problem_id=' + problem_id + '&vote=' + vote;
// The person is actually logged in so lets have him vote
$.ajax({
type: "POST",
url: "/problems/vote.php",
data: dataString,
success: function(html)
{
alert("data: " + html);
var class_name = ".votes_"+problem_id;
alert ("class name: " + class_name);
var x = $(class_name).val();
alert ("curr val: " + x);
$(class_name).val(html);
},
error : function(html)
{
errorMessage = html;
if (html == "not_logged_in")
{
queue.login = false;
//set the current problem id to the one within the dialog
$problemId.val(problem_id);
// Try to create the popup that asks user to log in.
// $dialog.dialog('open');
$("#loginpopup").dialog();
errorMessage = "";
// prevent the default action, e.g., following a link
return false;
}
else
if (errorMessage == "already_voted")
{
// Display a dialog box saying that the user already voted
$('<div />').html('You already voted this way on this problem.').dialog();
}
else
if ( errorMessage == "error_getting_vote")
{
$('<div />').html('Error getting existing votes.').dialog();
}
else
{
// ? :)
}
} // End of error case
}); // Closing AJAX call.
return false;
};
AJAX調用獲取作出並返回正確的值。問題是,我不能獲取和設置類votes_problem_id_value,我在我的PHP設置的問題ID的真正的價值在這裏的價值:
echo '<span class="half_text" style="color: #B77F37;">'.$problem_date.'</span> <span id="votes" class="votes_'.$problem_id.' half_text" style="padding-left: 10px;">'.$vote.'</span><strong> <a class="vote_up" style="font-size: 80.0%; color: #295B7B; font-weight:bold; text-decoration:none;" href="#" data-problem_id="'.$problem_id.'">Important</a></strong> | <strong><a class="vote_down" style="font-size: 80.0%; color: #295B7B; font-weight:bold; text-decoration:none;" href="#" data-problem_id="'.$problem_id.'">Not Important</a></strong>';
任何想法,我做錯了,怎麼來的我無法獲得該值或設置該span class =「votes _」的內容。$ problem_id。' half_text「
謝謝!
爲$(CLASS_NAME)的陣列? $(class_name)[0] .val(html);工作? – sissonb
@sissonb - '$(class_name)'是一個jQuery對象,而不是一個Array。 '$(class_name)[0]'是一個元素或未定義的',它們都沒有'.val()'方法。所以,不,這是行不通的。 – gilly3