繁体   English   中英

为什么在ajax调用完成后这个值为零(asp.net mvc 3和jQuery)?

[英]Why is this value zero after ajax call finishes (asp.net mvc 3 and jQuery)?

这是我的ajax电话:

  var totalCost = 0;
    function GetTotalCost(start, end, rID) 
    {
        $.ajax({
            url: '@Url.Action("CalculateTotalcost")',
            type: 'POST',
            data: JSON.stringify({ start:start, end:end, rID:rID}),
            dataType: 'json',
            processdata: false,
            contentType: 'application/json; charset=utf-8',
            success: function (data) { totalCost = data; }
//            error: function (xhr, ajaxOptions, thrownError) { $('.datepicker1').datepicker("hide"); },
//            complete: function (x, y) { $('.datepicker1').datepicker("refresh"); }
        });
    }

这是我调用ajax的函数:

 $('.datepicker2').datepicker({
            dateFormat: 'dd/mm/yy',
            firstDay: 1,
            yearRange: '2012:2100',
            beforeShowDay: function (date) {
                var day = date.getDate();
                if (day in alreadyTakenDays) {
                    return [false, '', alreadyTakenDays[day]];
                }
                else return [true, 'IsActive'];
            },
            onChangeMonthYear: function (year, month, inst) {
                alreadyTakenDays = {};
                getEvents(month, year);
            },

            onSelect: function (dateText, inst) {
                var end = dateText.substring(0, 2);
                console.log(end);
                var rID = $('#RoomID').val();
                console.log(rID);
                var startingHole = $('#DateOne').val();
                var start = startingHole.substring(0, 2);
                console.log(start);
                GetTotalCost(start, end, rID);

                document.getElementById('TotalCost').value = totalCost.toFixed(2);

            }
        });

执行jQuery脚本后,totalCount总是为0? 为什么会这样? 我该怎么办? 我想在ASP.NET MVC 3中指定totalCount为Html.TextBoxFor,这就是我需要totalCount的原因。 请帮忙。

当您在“TotalCost”文本框中设置值时,ajax调用是异步的并且尚未完成。

你应该有这样的成功函数处理程序来获得正确的总成本值:

function GetTotalCost(start, end, rID) 
    {
        $.ajax({
            url: '@Url.Action("CalculateTotalcost")',
            type: 'POST',
            data: JSON.stringify({ start:start, end:end, rID:rID}),
            dataType: 'json',
            processdata: false,
            contentType: 'application/json; charset=utf-8',
            success: function (data) { totalCost = data;
                                       document.getElementById('TotalCost').value = totalCost.toFixed(2); 
                                     }
        });
    }

在这样的代码中:

GetTotalCost(start, end, rID); 
document.getElementById('TotalCost').value = totalCost.toFixed(2);

第一行导致ajax调用,但它不等待响应,第二行立即执行。 之后,服务器响应后,执行success回调函数。 它将在第二行之后执行。 这就是您的总值错误的原因 - 更新TotalCost输入后,正确的值设置为totalCost

因为您的帖子是异步的,并且您正在尝试设置页面全局的js变量。 创建一个设置变量的函数并从回调中调用它,然后您将看到结果。

那是因为异步ajax请求。

处理事件时,请求尚未完成。

欢迎来到AJAX的美好世界。

暂无
暂无

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

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