0

我正在寻求以下帮助...

我有一个费用表格,用户可以在其中输入他们的里程索赔和发生的任何其他费用。里程费率是固定的,例如,如果您在 0-9 英里之间旅行,您将获得 5 英镑的固定费用,10-19 英里您获得 7 英镑,20-20 英里 9 英镑等。

我有一个小计 div,我想在里程值更改时更新它,但我的计算小计的函数似乎在 AJAX 调用完成之前执行以获取定价带。

小计功能基本上是:

function calculateClaim() {
    var mileageDistance = document.getElementById('journeyMileage').value;
    var expenseAmount1 = document.getElementById('expenseAmount1').value;
    var expenseAmount2 = document.getElementById('expenseAmount2').value;
    var expenseAmount3 = document.getElementById('expenseAmount3').value;
    if (mileageDistance != "") {
        calculateMileage = calculateMileageTotal(mileageDistance, function() {
            var mileageTotal = this;
            if (mileageTotal == '') {
                $("#mileageTotal").val(0.00);
                $("#journeyAmount").html('<strong>&pound;0.00</strong>');
                showDialog('alert','OK','','There is no mileage payment set up.','');
            } else {
                mileageTotal = parseFloat(mileageTotal).toFixed(2);
                $("#mileageTotal").val(mileageTotal);
                $("#journeyAmount").html('<strong>&pound;' + mileageTotal + '</strong>');
            }
        });
    } else {
        mileageTotal = 0;
        mileageTotal = mileageTotal.toFixed(2);
        document.getElementById('journeyAmount').innerHTML = '<strong>&pound;</strong>';
    }
    /* 
        SOME VALIDATION STUFF HERE FOR OTHER EXPENSE FIELDS
    */  
    subTotalCost = parseFloat(mileageTotal) + parseFloat(expenseAmount1) + parseFloat(expenseAmount2) + parseFloat(expenseAmount3);
    subTotalCost = subTotalCost.toFixed(2);
    document.getElementById('subTotal').innerHTML = '<strong>&pound;' + subTotalCost + '</strong>';
}

调用的calculateMileageTotal函数如下:

function calculateMileageTotal(mileage,callback) {
    var mileageDistance = mileage;
    var mileageTariffExists = $('#mileageTariffExists').val();
    if (mileageTariffExists == 1) {
       $.ajax({
            type: "POST",
            url: "./ajaxQueries.cfc?method=getExpensesMileageBand",
            data: ({
            competitionID: $("#compID").val(),          
            matchDate: $("#matchDate").val(),
            mileageTotal: mileageDistance
            }),
            dataType: "xml",
            success: function(xml) {
                $(xml).find('field').each(function(){
                    //alert((new XMLSerializer()).serializeToString(xml));
                    var mileageTotal = $(this).find('string').text();
                    callback.call(mileageTotal);
                });
            }
        });
    } else {
        showDialog('alert','OK','','There is no Mileage Rate available.','');
    }
}

getExpensesMileageBand 函数肯定可以正常工作并返回里程率,但它是认为mileageTotal 值为空的小计计算,因为它尚未从函数返回,但如果我放入几个警报,那么它可以工作(如它会减慢功能)。

有人有什么想法吗?我以前没有使用过回调,所以我不确定这是否是我出错的地方?

4

1 回答 1

0

为什么不将小计计算作为一个单独的函数,从回调的末尾或 else 语句的末尾调用。

function calculateSubtotal(milageTotal) {
  subTotalCost = parseFloat(mileageTotal) + parseFloat(expenseAmount1) + parseFloat(expenseAmount2) + parseFloat(expenseAmount3);
  subTotalCost = subTotalCost.toFixed(2);
  document.getElementById('subTotal').innerHTML = '<strong>&pound;' + subTotalCost + '</strong>';
}

if (mileageDistance != "") {
  calculateMileage = calculateMileageTotal(mileageDistance, function() {
    var mileageTotal = this;
    if (mileageTotal == '') {
      // existing code
    } else {
      // existing code
      calculateSubtotal(milageTotal);
    }
  });
} else {
  // existing code
  calculateSubtotal(milageTotal);
}
于 2013-10-16T09:44:18.683 回答