JQuery。表中每一行的Ajax请求

JQuery。表中每一行的Ajax请求,jquery,ajax,Jquery,Ajax,我尝试对表中的每一行执行Ajax请求,但未能达到预期的结果 表: <table> <tr class="data"> <td class="editable"> <a class="refresh btn btn-large" class="page"> Col one </a>

我尝试对表中的每一行执行Ajax请求,但未能达到预期的结果

表:

<table>
        <tr class="data">
            <td class="editable">
                <a class="refresh btn btn-large" class="page">
                    Col one
                </a>
            </td>
            <td class="editable">
                <a href="#" data-pk="10" id="query" class="query">
                    Col two
                </a>
            </td>
        </tr>
        <tr class="data">
            <td class="editable">
                <a class="refresh btn btn-large" class="page">
                    Col one 1
                </a>
            </td>
            <td class="editable">
                <a href="#" data-pk="10" id="query" class="query">
                    Col two 1
                </a>
            </td>
        </tr>
</table>
我的问题是: 所有ajax请求一次发送,但我需要在请求之间暂停


关于标签中的属性“id”:我应该使用它,因为“Bootstrap X编辑器”

看起来您只需要在
el
周围创建一个闭包:

(function(el) {
    $.ajax({
        url: 'wordstat/ajax?query='+query+'&page='+page,
        success: function(data){
            $(el).children('.editable').children('.relevantnost').html(data)
        }
    });
})(el);
这将为每个请求创建一个单独的
el
,因此每个响应将更新其相应的元素


(注意:如果您真的想在每个请求之间暂停,可以使用
$.ajax
async:false
选项,但我不明白您为什么要这样做。)

问题是,ajax请求是异步的,因此如果您在.each()中执行所有请求,就不会有暂停

您需要的是首先获取每个
el
元素并将它们放置在一个数组中。 然后创建一个全局变量作为计数器,以了解已发送的请求数

您发送第一个请求,然后在第一个成功函数中发送第二个请求,依此类推

您基本上需要重写,以便在上一个请求完成时发送请求

示例:

function test()
{
    var arr = new Array();
    var counter = 0;

    $('.data').each(function(i, el) {
        arr.push(el);       
    });

    doRequest(counter);

    function doRequest(counter)
    {
        var query   = $(arr[counter]).children('.editable').children('.query').text();
        var page    = $(arr[counter]).children('.editable').children('.page').text();

        $.ajax({
            url: 'http://www.google.com?'+query+'&page='+page,
            success: function(data){
                alert("made request with query="+ query);   
                counter++;
                if(counter<arr.length)
                doRequest(counter);
            }
        });
    }

}
功能测试()
{
var arr=新数组();
var计数器=0;
$('.data')。每个(函数(i,el){
方位推力(el);
});
doRequest(柜台);
函数doRequest(计数器)
{
var query=$(arr[counter]).children('.editable').children('.query').text();
var page=$(arr[counter]).children('.editable').children('.page').text();
$.ajax({
网址:'http://www.google.com?“+query+”&page=“+page,
成功:功能(数据){
警报(“使用query=“+query”发出请求);
计数器++;

如果(counter

它们的AJAX请求是异步的,那么一旦一个AJAX请求被触发,下一个
每个()
迭代将启动,这样您可能会有许多对AJAX端点的并行请求。如果希望请求被阻止,您可以在配置中使请求异步。这将使它们以串行方式运行

        $.ajax({
            async: false,
            url: 'wordstat/ajax?query='+query+'&page='+page,
            success: function(data){
                $(el).children('.editable').children('.relevantnost').html(data)
            }
        });

为什么需要暂停?因为在请求之间,我应该做一些工作,突出显示当前行,等等@Бццццццццццццццццццццццppen位于索引i-1处的元素上,该元素将“阻塞”在当前项上进一步执行成功回调。Mb你说得对,我真的不需要它。但是我想检查它,我想知道怎么做。你的答案非常重要。是的,你的答案非常好。我输入了代码
async:false
,这是我所需要的。对不起,如果我在某个地方出错,英语很糟糕(但我在任何地方都会指出我的错误:D)我考虑过……谢谢,我会尝试;)这种方法可能对不支持
async:false
或不可取的情况很有用:“跨域请求和数据类型:”jsonp“请求不支持同步操作。请注意,同步请求可能会暂时锁定浏览器,在请求处于活动状态时禁用任何操作。”我将此脚本用于“跨域请求”。然后我不应该使用
async:false
?或者我误解了(如果是这种情况,请抱歉)您?P.s.
dataType:
not jsonpasync:false不适用于跨域请求。跨域是指您的javascript在一个域上运行,而您试图访问另一个域。例如,如果您的页面处于打开状态,并且您试图从中访问数据。我认为在您的情况下,所有内容都在同一台服务器上,因此
async:false
哦,那我就误解了。因为:ajax确实请求本地脚本,它使用cUrl从另一台服务器解析页面。然后,我想,我可以使用
async:false
        $.ajax({
            async: false,
            url: 'wordstat/ajax?query='+query+'&page='+page,
            success: function(data){
                $(el).children('.editable').children('.relevantnost').html(data)
            }
        });