简体   繁体   English

为什么我的Ajax函数无法在目标div元素上附加html?

[英]Why my ajax function cannot append html at targeted div element?

I am submitting a html form through AJAX and then appending results at particular div element.The corresponding ajax is :- 我通过AJAX提交html表单,然后将结果附加到特定的div元素上。对应的ajax是:-

$(document).ready(function(){
$('.commentbutton').click(function(){
    var idb=$(this).attr('id');
    var formid=$('#Comment'+idb);
    datab=getFormData(formid);
    $.ajax({
        type:"POST",
        url:'/submit/channelcomment',
        data:datab,
        success:function(data){
          console.log(data.content);
          console.log(data.profile);
          var html="<div class='CommentRow'><a href='/Channel/"+data.profile+"/'style='font-weight: bolder;margin-right: 10px;display: inline-block;'>"+data.profile+"</a>"+data.content+"</div>"
          console.log('Done');
          idt=$('#CommentBody'+idb);
          console.log(idt);
          idt.append(html);
        },

    }),
    event.preventDefault();
});
  });
  function getFormData($form){
var unindexed_array = $form.serializeArray();
var indexed_array = {};

    $.map(unindexed_array, function(n, i){
    indexed_array[n['name']] = n['value'];
    });

return indexed_array;
}

The desired position at which i'm trying to append html is as follows:- 我要附加html的所需位置如下:-

<div class="CommentBody" id="CommentBody{{c.id}}">
</div>

Here c.id and idb equals to 1.But it is not appending html. 此处c.id和idb等于1,但未附加html。

When you say 当你说

But it is not appending html. 但是它没有附加html。

What is actual behavior? 实际行为是什么?

I tried the dummy code as below and it is working fine. 我尝试了如下的伪代码,并且工作正常。

 $(document).ready(function() { $('.commentbutton').click(function() { var html = "<div class='CommentRow'><a href='/Channel/data.profile/'style='font-weight: bolder;margin-right: 10px;display: inline-block;'>data.profile</a>data.content</div>" var idb = '1'; idt = $('#CommentBody' + idb); alert(idt); idt.append(html); }); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button class='commentbutton'>Comment</button> <div class="CommentBody" id="CommentBody1"> </div> 

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

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