简体   繁体   English

AJAX似乎正在缓存我的结果

[英]AJAX appears to be caching my results

This is a stub from another post located here: Dynamically added content no longer opens accordion 这是位于此处的另一篇文章的存根: 动态添加的内容不再打开手风琴

The issue I am having is that my database result set appears to be cached in JQuery. 我遇到的问题是我的数据库结果集似乎已缓存在JQuery中。 I have added cache:false to the AJAX call but the results appear the same for any element I click on which is not correct. 我在AJAX调用中添加了cache:false ,但是对于单击不正确的任何元素,结果看起来都是一样的。 Every element has it's own ID and will return a different result set. 每个元素都有其自己的ID,并将返回不同的结果集。 I'm not sure why this is happening, but is there something that I am missing, or something else that I can check? 我不确定为什么会这样,但是是否有我遗漏的东西,或者可以检查的其他东西?

This is the AJAX call so far: 到目前为止,这是AJAX调用:

  ...
  success : function(data)
  {
    $('<div/>', {
      id:'modal'
    }).html(data).appendTo('body');

    $('#modal').popup({
      autoopen  : true,
    });

    $('.accordion').accordion({
        collapsible: true,
    });
  },

Basically, data carries the results being returned from my database. 基本上, data包含从数据库返回的结果。 I have checked the results in Firebug and can see each element has the right data, but it's not rendering that way. 我已经在Firebug中检查了结果,可以看到每个元素都具有正确的数据,但是并没有那样渲染。

EDIT: 编辑:

$.ajax({
url  : "modal.asp",
type : "POST",
cache: false,
data : $(this).data('cid'),
success : function(data)
{
  $('<div/>', {
    id:'modal'
  }).html(data).appendTo('body');

  $('#modal').popup({
    autoopen  : true,
  });

  $('.accordion').accordion({
    collapsible: true,
  });
}

The problem is that you are not removing your old #modal div(s), therefore they accumulate. 问题是您没有删除旧的#modal div,因此它们会累积。

Multiple DOM elements with same id are invalid HTML and the effect of selecting $('#modal) is indeterminate. 具有相同id的多个DOM元素是无效的HTML,并且选择$('#modal)的效果不确定。 It's probably selecting the first one ... repeatedly 可能是反复选择第一个...

This code: 这段代码:

$('<div/>', {
    id:'modal'
    }).html(data).appendTo('body');

Will create a new div element with an id of modal . 将创建一个ID为modal的新div元素。

$('div#modal').html(data).appendTo('body');

This would modify your modal content. 这将修改您的模式内容。

If your element doesn't exist at first call, just do this: 如果您的元素在第一次调用时不存在,请执行以下操作:

if($('div#modal').length > 0){
    $('div#modal').html(data).appendTo('body');
} else {
    $('<div/>', {
        id:'modal'
       }).html(data);
}
$.ajax({
        url  : "modal.asp",
        type : "POST",
        cache: false,
        data : $(this).data('cid'),
    success : function(data)
    {
        var newData = "<div>" + data + "</div>";
        $('.accordion').html(newData)
        $('.accordion').accordion("refresh");    

        $('#modal').modal('show');      
    }

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

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