简体   繁体   English

AJAX成功后追加DIV

[英]Append DIV after AJAX Success

As the title states, I am trying to perform a simple task of when my ajax post successfully completes my database query, I would like to add the word "Success" into an empty DIV using the AJAX success option. 如标题所示,我试图执行一个简单的任务,当我的ajax帖子成功完成我的数据库查询时,我想使用AJAX成功选项将单词“ Success”添加到一个空的DIV中。 I can't figure out how to get that text to appear. 我不知道如何使该文本出现。 Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

Here is my working AJAX post: 这是我工作的AJAX帖子:

<script>
    $(function(){
        $("#noteForm").submit(function(){
            // prevent native form submission here
            $.ajax({
                type: "POST",
                data: $('#noteForm').serialize(),
                dataType: 'json',
                url: "actionpages/link_notes_action.cfm?id=2",
                success: function() {
                    $(".response").append($( "Success" ) );
                }    
            });
            return false;           
        });
    });
</script>

I have a div in my page with id of "response" that is nested inside the div of my form. 我的页面上有一个ID为“ response”的div,它嵌套在表单的div中。

<!--- Modal HTML embedded directly into document --->
<div id="LinkNotes#id#" style="display:none;">
    <p>These are the notes for: #link_description#</p>
    <form id="noteForm">
        <textarea id="noteText" name="noteText" cols="45" rows="10">#notes#</textarea><br />

        <input name="submit" id="submitForm" type="submit" value="Update"><div class="response" id="response"></div>
        <input type="hidden" name="hidden" value="#get_dashboard_links.ID#">
    </form>
</div>

So close! 很近!

Since you just want to append a string, you only have to pass that string to .append . 由于您只想附加一个字符串,因此只需要将该字符串传递给.append

Replace: 更换:

.append($( "Success" ) );

With: 带有:

.append("Success");

$("Success") looks for a <Succes /> tag, which it doesn't find, resulting in an empty set that's being appended. $("Success")查找一个<Succes />标记,但找不到该标记,从而导致添加了一个空集。
As you can probably imagine, appending "nothing" doesn't do particularly much ;-) 如您所料,添加“ nothing”并没有多大作用;-)

I was able to find the answer from this post 我可以从这篇文章中找到答案

Apparently removing 显然删除

dataType: 'json',

from my script fixed it. 从我的脚本修复它。

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

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