2011-11-08 28 views
1

獲得通過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「

謝謝!

+0

爲$(CLASS_NAME)的陣列? $(class_name)[0] .val(html);工作? – sissonb

+0

@sissonb - '$(class_name)'是一個jQuery對象,而不是一個Array。 '$(class_name)[0]'是一個元素或未定義的',它們都沒有'.val()'方法。所以,不,這是行不通的。 – gilly3

回答

2

.val()是表單元素,而不是跨度。使用.text()(或可能是.html())來獲取跨度中包含的文本。

var x = $(class_name).text(); 

或者,要設置一個跨度的自定義屬性,使用.attr()

var x = $(class_name).attr("value"); 
... 
$(class_name).attr("value", html); 

或者自定義數據與跨度關聯,請使用.data()

var x = $(class_name).data("value"); 
... 
$(class_name).data("value", html); 
+0

只是試圖用.text()來完成它,但仍然無法工作。還有什麼可能會出錯的? – GeekedOut

+0

其實剛剛得到它的工作!將在2分鐘內接受。 – GeekedOut

-1

我見您的PHP正在處理POST值,比如它們是全局變量。如果您的register_globals設置已關閉,則不會設置$ vote和$ problem_id,除非您明確設置它們。最好的做法是關閉register_globals,然後使用超全局$ _POST。

因此,請嘗試class="votes_'.$_POST['problem_id'].' half_text"

這裏有register_globals的PHP文檔:

+0

您是如何得出「register_globals」有問題的? OP僅顯示了部分PHP代碼,負責生成部分HTML。我給-1 – Tadeck