javascript – How can I fix the html error in the jquery post?-ThrowExceptions

Exception or error:

I am responding by sending a post in jquery. But div also writes when the div is closed and when the div is opened.

enter image description here

I need to leave only after closing
the code I wrote

<script>
jQuery(document).ready(function() {
  jQuery(".button").bind("click", function() {

    var name = jQuery('.nameField').val();
    var surname = jQuery('.surnameField').val();
    var age = jQuery('.ageField').val();

    jQuery('.surnameField').val('');

    jQuery.ajax({
      url: "../ajax/db.php",
      type: "POST",
      data: {
        name: name,
        surname: surname,
        age: age
      }, // Передаем данные для записи
      dataType: "json",
      success: function(result) {
        if (result) {
          jQuery('.comments-list div').remove();
          jQuery('.comments-list').append(function() {
            var res = '';

            for (var i = 0; i < result.users.name.length; i++) {
              res += '<div class="media last-child"><a class="media-left" href="#">' + result.users.id[i] + '</a><div class="media-body"><h4 class="media-heading user_name">' + result.users.name[i] + '<small>' + result.users.age[i] + '</small></h4><p>' + result.users.surname[i] + '<p></div></div><hr class="invis">';
            }
            return res;
          });
          console.log(result);
        } else {
          alert(result.message);
        }
        return false;
      }
    });
    return false;
  });
});
</script>
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *