如何在jQuery中正确追加表?

时间:2019-02-25 14:49:31

标签: javascript jquery

我遇到一个问题,我要使用jQuery附加到表中,还要附加到<div class="jobEntity" id="bestTable">中。出于某种原因,我不想这样附加表格。有没有一种方法可以解决我的jQuery代码以正确追加表的问题?感谢您的帮助。

{request.contextPath}-只是localhost:8080 /

var cache = {};

$("#searchTextField").autocomplete({
  minLength: 2,
  focus: function(event, ui) {
    event.preventDefault();
  },
  source: function(request, response) {
    var term = request.term;
    if (request.term in cache) {
      response(cache[request.term]);
      return;
    }

    $.ajax({
      url: "#{request.contextPath}/JobSearchItem.xhtml",
      type: "GET",
      data: {
        term: request.term
      },
      dataType: "json",
      success: function(data) {
        response(data);
      }
    });
  },
  select: function select(event, ui) {
    event.preventDefault();
    var url = '#{request.contextPath}/index.xhtml';
    var searchValue = ui.item.value;
    var data = new FormData();
    data.append('searchValue', searchValue);
    $.ajax({
      url: url,
      data: data,
      method: "POST",
      processData: false,
      contentType: false,
      cache: false
    }).done(function(text) {
      $('#results').append($(text).find('#bestTable'));
      $('#results').append($(text).find('#textTable'));
      $('#results').append($(text).find('table'));
      $("#clearone").show();
    });
  },
  response: function response(event, ui, data) {
    if (!ui.content.length) {
      var message = {
        value: "",
        label: "NOTHING HAS BEEN FOUND"
      };
      ui.content.push(message);
    }
  }
});

1 个答案:

答案 0 :(得分:1)

好吧,你可以做这样的事情:

$.ajax({
      url: url,
      data: data,
      method: "POST",
      processData: false,
      contentType: false,
      cache: false
    }).done(function(text) {
      let append = $(text).find('#bestTable').text() + $(text).find('#textTable').text() + $(text).find('table').text();
      $('#results').append(append);
      $("#clearone").show();
    });

首先,请准备附加内容,然后立即附加。