2011-10-19 60 views
0

我想用jQuery來修改一些來自AJAX的內容,但由於某種原因它似乎無法訪問內容。我對jQuery仍然很陌生,所以我不確定哪裏出了問題,並希望這裏有人能指出我的方向。AJAX後通過jQuery修改內容

所有的代碼位於GitHub上這裏https://github.com/Ryan-Myers/WordPress-Survey-Plugin/blob/master/survey-js.php

但相關代碼如下(這是一個WordPress插件BTW的一部分):

//Add a new question to the passed survey 
function add_question(survey_id) { 
    var data = { 
     action: 'survey_add_question_ajax', 
     security: '<?php echo wp_create_nonce("survey_add_question_nonce"); ?>', 
     survey: survey_id 
    }; 

    jQuery.post(ajaxurl, data, function(response) { 
     jQuery('#survey-admin-page').html(response); 
     jQuery('#survey-questions-table').slideUp(); 
     jQuery('#survey-add-question').slideDown(); 
    }); 

    //FAILS HERE 
    jQuery('#survey-add-question').ready(function(){ 
     //Default to hiding the question setup until they select a question type. 
     jQuery('#questionsetup').hide(); 
     jQuery('#save_question').attr('onclick', 'submit_question('+survey_id+')'); 

     //Testing to see if it will return a usable value, which it doesn't. 
     alert(jQuery('#save_question').attr('onclick')); 
    }); 
} 

回答

2

$。就緒不起作用像這樣:http://api.jquery.com/ready/

.ready()方法只能在匹配當前文檔的jQuery對象上調用,所以選擇器可以省略。

您通常使用它來知道DOM何時準備就緒,然後開始執行依賴於DOM的代碼。

您可以添加的代碼無法正常工作時的$。員額回調塊的了slideDown後,也可以添加一個回調了slideDown的了slideDown後將火齊全:http://api.jquery.com/slideDown/

+0

+1爲slideDown的回調,我認爲這是需要在這裏。 – GregL

+0

經過漫長的夜晚,我感覺很累,而且聽起來聲音很好。我只是想說你是對的,並在早上覈實。非常感謝你 :) – Ryan

0

是否動態加載了#save_question?這就是爲什麼你使用jQuery('#survey-add-question'),ready()。

例如

$('#save_question').live("click", function() { 
    submit_question(survey_id); 
}); 

如果是這樣你可能會想看看jQuery.live

如果你只是想等待後運行您的代碼之前fininsh把你的代碼jQuery.post函數內部

jQuery.post(ajaxurl, data, function(response) { 
     // code in here will be called once the response is returned from AJAX 

    });