繁体   English   中英

在AJAX帖子之后通过jQuery修改内容

[英]Modifying content through jQuery after an AJAX post

我正在尝试使用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'));
    });
}

$ .ready不能那样工作: http//api.jquery.com/ready/

.ready()方法只能在与当前文档匹配的jQuery对象上调用,因此可以省略选择器。

您通常使用它来了解DOM何时就绪,然后开始执行依赖于DOM的代码。

您可以在slideDown之后添加对$ .post回调无效的代码块,也可以在slideDown完成后添加回调给slideDown: http://api.jquery .COM /了slideDown /

#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

    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM