Javascript 循环中的多个ajax内容请求
我试图将一些ajax内容加载到一个表中,不幸的是,它只是多次加载最后一行,而不是加载每一个新行 这是我正在使用的代码:Javascript 循环中的多个ajax内容请求,javascript,jquery,ajax,while-loop,Javascript,Jquery,Ajax,While Loop,我试图将一些ajax内容加载到一个表中,不幸的是,它只是多次加载最后一行,而不是加载每一个新行 这是我正在使用的代码: function periodicRefresh() { $.ajax({ type: 'GET', url: 'include/ajaxActions.php', data: "action=displayLastEvent", success: function(msg){ v
function periodicRefresh()
{
$.ajax({
type: 'GET',
url: 'include/ajaxActions.php',
data: "action=displayLastEvent",
success: function(msg){
var newid = msg;
current = $("#list tr:first").get(0).id;
if(newid != current){
while (current<newid)
{
current++;
addToList(current);
}
}
}
});
}
function addToList(x)
{
$.ajax({
type: 'GET',
url: 'include/ajaxActions.php',
data: "action=displayRow&row="+x,
success: function(msg){
$("#list").prepend(msg);
$("#list tr:first").highlightFade({
speed:3000
});
lastrow = x-20;
$('#'+lastrow).remove();
}
});
}
函数周期refresh()
{
$.ajax({
键入:“GET”,
url:'include/ajaxActions.php',
数据:“action=displayLastEvent”,
成功:功能(msg){
var newid=msg;
当前=$(“#list tr:first”).get(0.id;
如果(newid!=当前){
虽然(当前您需要将xmlHttps推入数组或抽象数据类型,然后可以附加事件处理程序。jquery似乎不适合您。我建议您改变方法,因为每当需要添加更多行时,您可能会发出更多的AJAX请求列表(2+而不是2)
我会更新include/ajaxActions.php?action=displayRow
以接受一个CSV的ID(或您传入的任何内容),并返回一组行数据,而不是一行的数据。我认为:
current = $("#list tr:first").get(0).id;
始终返回与jQuery相同的结果
仅记住第一次加载页面时的结果。
例如,如果您有一个tr[id=0]
:
pass 1 : current = 0; msg = 1 -> 1 tr prepended with id = 1;
pass 2 : current is always 0 (not 1); msg = 1 -> 1 tr prepended with id = 1;
...
您应该做的是,让jQuery
在添加消息后识别您的页面结构,或者以不同的方式存储最后一个索引:使用隐藏输入
,例如:
HTML:
将periodicresh
修改为#lastId值
:
function periodicRefresh()
{
$.ajax({
type: 'GET',
url: 'include/ajaxActions.php',
data: "action=displayLastEvent",
success: function(msg){
var newid = msg;
var current = $("#lastId").val();
if(current<newid) $("#lastId").val(newid);
if(newid != current){
while (current<newid)
{
current++;
addToList(current);
}
}
}
});
}
函数周期refresh()
{
$.ajax({
键入:“GET”,
url:'include/ajaxActions.php',
数据:“action=displayLastEvent”,
成功:功能(msg){
var newid=msg;
var current=$(“#lastId”).val();
如果(当前)
$(document).ready(function(){
$("#lastId").val(0);//or by using your displayLastEvent action
});
function periodicRefresh()
{
$.ajax({
type: 'GET',
url: 'include/ajaxActions.php',
data: "action=displayLastEvent",
success: function(msg){
var newid = msg;
var current = $("#lastId").val();
if(current<newid) $("#lastId").val(newid);
if(newid != current){
while (current<newid)
{
current++;
addToList(current);
}
}
}
});
}