简体   繁体   English

如何将ajax结果附加到目标div ID?

[英]How to append the ajax result to the target div id?

I am using the following ajax code to get the result form another one php, now I need to append the result to the appropriate div(preview) id 我正在使用以下ajax代码从另一个php获取结果,现在我需要将结果附加到适当的div(preview)id

$("#imageform").ajaxForm({
    target: '#preview'
}).submit();

If you are using jQuery Form Plugin, Use like this..data is the data which you are sending from php file. 如果您使用的是jQuery Form Plugin,则使用如下所示。.data是您从php文件发送的数据。

$("#imageform").ajaxForm({
            clearForm: 'true',
            success: function(data){
            if(data != '')
            {
                $("#preview" ).append(data);
            }
            }
        }).submit();

Use the success callback function and don't specify the target option. 使用success回调函数,不要指定target选项。

$("#imageform").ajaxForm({
    success: function(d,s,x){
       $("#preview").append(d);
    }
}).submit();

The problem is that the target option is used to "replace" the target (or contents of the target) along with the replaceTarget option. 问题在于target选项与replaceTarget选项一起用于“替换”目标(或目标的内容)。 But since what you need to "append" instead of "replace", you should avoid the whole target / replaceTarget mechanism 但是由于您需要“附加”而不是“替换”,因此应避免使用整个target / replaceTarget机制

If you are using jQuery Form Plugin , according to it's documentation you can use 如果您使用的是jQuery Form Plugin ,则根据其文档可以使用

success API: Callback function to be invoked after the form has been submitted. success API:提交表单后将调用的回调函数。 If a 'success' callback function is provided it is invoked after the response has been returned from the server. 如果提供了“成功”回调函数,则在从服务器返回响应后将调用它。

$("#imageform").ajaxForm({
    target: '#preview',
    success: function() { 
        $('#preview').fadeIn('slow'); 
    } 
}).submit();

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

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