Почему моя функция ajax не может добавить html в целевой элемент div? - PullRequest
0 голосов
/ 17 марта 2019

Я отправляю html-форму через AJAX, а затем добавляю результаты в определенный элемент 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;
}

Желаемая позиция, в которой я пытаюсь добавить HTML, выглядит следующим образом: -

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

Здесь c.id и idb равны 1. Но это не добавление html.

1 Ответ

1 голос
/ 17 марта 2019

Когда вы говорите

Но он не добавляет html.

Что такое реальное поведение?

Я попробовал фиктивный код, как показано ниже, иработает нормально.

$(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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...