Jquery 为什么我的祖先感到困惑?

Jquery 为什么我的祖先感到困惑?,jquery,Jquery,我更新一行中的三个输入,它们通过单击一个按钮按顺序更新,更新完成后,我对每个输入进行冷色调更改(发光)效果: 你应该看到这样的东西,其中t=时间(我觉得自己像个科学家) 你们怎么想 谢谢 您正在执行一个ajax请求,每个请求可能需要不同的时间。。。由于ajax是异步的,它们都在同一时间执行,哪个先返回第一个输出,等等。您正在执行一个ajax请求,每个请求可能需要不同的时间。。。由于ajax是异步的,它们都同时执行,其中一个首先返回第一个输出,等等。尝试使用队列如我的回答中所示,然后您可以执行以下

我更新一行中的三个输入,它们通过单击一个按钮按顺序更新,更新完成后,我对每个输入进行冷色调更改(发光)效果:

你应该看到这样的东西,其中t=时间(我觉得自己像个科学家)

你们怎么想


谢谢

您正在执行一个ajax请求,每个请求可能需要不同的时间。。。由于ajax是异步的,它们都在同一时间执行,哪个先返回第一个输出,等等。

您正在执行一个ajax请求,每个请求可能需要不同的时间。。。由于ajax是异步的,它们都同时执行,其中一个首先返回第一个输出,等等。

尝试使用
队列
如我的回答中所示,然后您可以执行以下操作:

function UpdatePrice(TheButton, Type) {
    var q = new Queue;
    $(TheButton).parent().parent().find("input").each(function (index, element) {
        //add to the Queue
        q.add(function(){
            $.ajax({
                cache: false,
                data: { ByContractID: $(element).parent().attr("id"), Cost: $(element).val(), ItemType: Type },
                type: "Post",
                url: '@Url.Action("UpdateCost")'
            }).success(function () {

                $(element).next().html(($(TheButton).parent().parent().children("td").eq(0).html() *     $(element).val()).toFixed(2));
                $(element).val(parseFloat($(element).val()).toFixed(2));
                var old = $(element).css('background-color');
                $(element).animate({ "background-color": "#D2F0C9" }, "fast", function () {     $(element).animate({ "background-color": old }, "fast") });

                q.next(); //run next function

            });

            return false; 
            // ^^ insures that the queue does not continue when the function is finished.
        });

    });

    return false;
}

之所以必须这样做,是因为Ajax是异步的,所以为了按顺序运行它们,您必须在上一个调用完成后运行新的Ajax调用。

尝试使用我的答案中类似的
队列,然后您可以这样做:

function UpdatePrice(TheButton, Type) {
    var q = new Queue;
    $(TheButton).parent().parent().find("input").each(function (index, element) {
        //add to the Queue
        q.add(function(){
            $.ajax({
                cache: false,
                data: { ByContractID: $(element).parent().attr("id"), Cost: $(element).val(), ItemType: Type },
                type: "Post",
                url: '@Url.Action("UpdateCost")'
            }).success(function () {

                $(element).next().html(($(TheButton).parent().parent().children("td").eq(0).html() *     $(element).val()).toFixed(2));
                $(element).val(parseFloat($(element).val()).toFixed(2));
                var old = $(element).css('background-color');
                $(element).animate({ "background-color": "#D2F0C9" }, "fast", function () {     $(element).animate({ "background-color": old }, "fast") });

                q.next(); //run next function

            });

            return false; 
            // ^^ insures that the queue does not continue when the function is finished.
        });

    });

    return false;
}

之所以必须这样做,是因为Ajax是异步的,所以为了按顺序运行它们,必须在上一个调用完成后运行新的Ajax调用。

也许可以链接到JSFIDLE?Ajax响应是异步的。触发成功的顺序可以是不同的。也许您可以链接到JSFIDLE?Ajax响应是异步的。触发成功的顺序可以是不同的顺序。未捕获引用错误:队列不可用defined@Pinch您是否从我的@Pinch添加了
队列
类?@Pinch没问题^。
队列
对象可以以多种不同的方式使用。我相信您会找到扩展它的方法:-duncapted ReferenceError:Queue不是defined@Pinch您是否从我的@Pinch添加了
队列
类?@Pinch没问题^。
队列
对象可以以多种不同的方式使用。我相信你会找到扩展它的方法
function UpdatePrice(TheButton, Type) {
    $(TheButton).parent().parent().find("input").each(function (index, element) {

        $.ajax({
            cache: false,
            data: { ByContractID: $(element).parent().attr("id"), Cost: $(element).val(), ItemType: Type },
            type: "Post",
            url: '@Url.Action("UpdateCost")'
        }).success(function () {

            $(element).next().html(($(TheButton).parent().parent().children("td").eq(0).html() * $(element).val()).toFixed(2));
            $(element).val(parseFloat($(element).val()).toFixed(2));
            var old = $(element).css('background-color');
            $(element).animate({ "background-color": "#D2F0C9" }, "fast", function () { $(element).animate({ "background-color": old }, "fast") });


        });


    });

    return false;
}
function UpdatePrice(TheButton, Type) {
    var q = new Queue;
    $(TheButton).parent().parent().find("input").each(function (index, element) {
        //add to the Queue
        q.add(function(){
            $.ajax({
                cache: false,
                data: { ByContractID: $(element).parent().attr("id"), Cost: $(element).val(), ItemType: Type },
                type: "Post",
                url: '@Url.Action("UpdateCost")'
            }).success(function () {

                $(element).next().html(($(TheButton).parent().parent().children("td").eq(0).html() *     $(element).val()).toFixed(2));
                $(element).val(parseFloat($(element).val()).toFixed(2));
                var old = $(element).css('background-color');
                $(element).animate({ "background-color": "#D2F0C9" }, "fast", function () {     $(element).animate({ "background-color": old }, "fast") });

                q.next(); //run next function

            });

            return false; 
            // ^^ insures that the queue does not continue when the function is finished.
        });

    });

    return false;
}