2011-10-19 58 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 

    });