如何更正此JavaScript添加功能?

时间:2013-02-23 02:38:35

标签: javascript input add validation

我有一个需要六个输入的表单(现在还有更多但只有这些内容)

原始票价

原税

新票价

新税

来宾人数

  1. 当按下“计算”时,我使用javascript来添加原始票价 和原始税以获得原始总数

  2. 然后我添加新票价和新税以获得新总票数

  3. 现在我将原始总数与新总数进行比较

  4. 如果原始总数大于新总数,如果有多种方法可以使用一个方法来完成我需要的数学运算并将结果设置为显示

  5. 我最初使用这些值测试所有内容:

    原始票价= 1000 原税= 200

    新票价= 800 新税= 200

    费用= 150 客人人数= 3

    可以在这里看到https://pnrbuilder.com/_popups/exchangeMyPNR_MATH_TEST_2.php (页面仅适用于Chrome) 以上工作完全符合预期,但当我将值更改为:

    原始票价= 949.83 原税= 321.18

    新票价= 453.91 新税= 143.91

    费用= 150 客人人数= 3

    在这里看到https://pnrbuilder.com/_popups/exchangeMyPNR_MATH_TEST.php (页面仅适用于Chrome) 此测试使用错误的if语句来完成剩下的数学

    我不明白为什么会这样,因为原始总数仍然是>新的总数,所以它应该使用与第一个例子相同的方法。我发出警报,让我确切地知道哪个if语句被用来做数学运算,显然这里使用了错误的语句,但我只是想弄清楚原因。

    我知道这很复杂但有人可以帮我弄清楚我的逻辑出错了吗?


    以下是完整功能:

    function addMe(frm) {
        var orBase = Number(frm.box1.value);
        var orTax = Number(frm.box2.value);
        var nwBase = Number(frm.box3.value);
        var nwTax = Number(frm.box4.value);
        var fee = Number(frm.fee.value);
        var gsts = Number(frm.guests.value);
    
    
        var fltrd_orBase = orBase * 100;
        var fltrd_orTax = orTax * 100;
        var fltrd_orTtl = fltrd_orBase + fltrd_orTax;
        var orTtl = fltrd_orTtl / 100;
        var final_orTtl = orTtl.toFixed(2)
        frm.result.value = orTtl.toFixed(2)
    
        var fltrd_nwBase = nwBase * 100;
        var fltrd_nwTax = nwTax * 100;
        var fltrd_nwTtl = fltrd_nwBase + fltrd_nwTax;
        var nwTtl = fltrd_nwTtl / 100;
        var final_nwTtl = nwTtl.toFixed(2)
        frm.result2.value = nwTtl.toFixed(2)
    
        var e = document.getElementById("residual");
        var selectVal = e.options[e.selectedIndex].value;
    
    
    
        if (final_orTtl <= final_nwTtl) {
    
            document.getElementById("forfeitTable").style.display="none";
            document.getElementById("MCOtable").style.display="none";       
    
            var undiff = final_nwTtl - final_orTtl;
            var diff =undiff
            document.getElementById("differenceDisplay").innerHTML=diff.toFixed(2);
            frm.difference.value = diff.toFixed(2);
    
            var ppCost = diff + fee;
            frm.pptotal.value = ppCost.toFixed(2);
            document.getElementById("ppDisplay").innerHTML=ppCost.toFixed(2);
    
            var ttlCost = ppCost * gsts;
            frm.totalcost.value = ttlCost.toFixed(2);
            document.getElementById("grandTotalDisplay").innerHTML=ttlCost.toFixed(2);              
    
            // this is just for testing to show which method was actually used
            if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                        // this is in its own if statement so it doesnt popuop while entering data
                        alert('if 1 was used');
                    }       
    
        }
    
    
        else if (final_orTtl > final_nwTtl) {
    
    
    
              if (selectVal == "residualX" || selectVal == "residualN") {
    
                  document.getElementById("MCOtable").style.display="none";
                  var diff = final_orTtl - final_nwTtl;
    
                  var displayDiff = diff* -1;
                  document.getElementById("differenceDisplay").innerHTML= displayDiff.toFixed(2);
                  frm.difference.value = displayDiff.toFixed(2);
    
    
    
    
                  document.getElementById("forfeitInfo").innerHTML = "Beyond the cost above";
                  frm.lost.value = diff.toFixed(2);
                  document.getElementById("ppForfeitedDisplay").innerHTML = diff.toFixed(2);
    
                  var ttlfForfeited = diff * gsts;
                  frm.lostTTL.value = ttlfForfeited.toFixed(2);
                  document.getElementById("totalForfeitedDisplay").innerHTML=ttlfForfeited.toFixed(2);
    
    
    
                  var ppCost = fee;
                  frm.pptotal.value = ppCost.toFixed(2);
                  document.getElementById("ppDisplay").innerHTML=ppCost;
    
                  var ttlCost = fee * gsts;
                  frm.totalcost.value = ttlCost.toFixed(2);
                  document.getElementById("grandTotalDisplay").innerHTML=ttlCost;
    
                    if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                        document.getElementById("forfeitTable").style.display="table";
                    }
                    // this is just for testing to show which method was actually used 
                    if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                        // this is in its own if statement so it doesnt popuop while entering data
                        alert('if 2.1 was used');
                    }
              }
    
              // this is the method that should be used below
              else if (selectVal == "residualA" ) {
    
                  document.getElementById("MCOtable").style.display="none";
                  var diff = final_orTtl - final_nwTtl;
    
                  var displayDiff = diff* -1;
                  document.getElementById("differenceDisplay").innerHTML= displayDiff.toFixed(2);
                  frm.difference.value = diff.toFixed(2);
    
                   if ( diff > fee) {
    
                        var residual =  diff - fee ;
    
                        document.getElementById("forfeitInfo").innerHTML = "No additional cost. However,";
                        frm.lost.value = residual.toFixed(2);
                        document.getElementById("ppForfeitedDisplay").innerHTML = residual.toFixed(2);
    
                        var ttlfForfeited = residual * gsts;
                        frm.lostTTL.value = ttlfForfeited.toFixed(2);
                        document.getElementById("totalForfeitedDisplay").innerHTML=ttlfForfeited.toFixed(2);
    
                        //document.getElementById("differenceDisplay").innerHTML=0;
                        frm.difference.value = diff;
    
                        document.getElementById("ppDisplay").innerHTML=0;
                        document.getElementById("grandTotalDisplay").innerHTML=0;
    
                           if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                              document.getElementById("forfeitTable").style.display="table";
                          }
                          // this is just for testing to show which method was actually used 
                          if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                            // this is in its own if statement so it doesnt popuop while entering data
                            alert('if 2.2 was used');
                          }
    
    
    
                    }
                  else  {
    
                        var remBal =  fee - diff ;
    
                        var ttlcost = remBal * gsts;
                        frm.totalcost.value = ttlcost.toFixed(2);
    
                        document.getElementById("ppDisplay").innerHTML=remBal.toFixed(2);
                        document.getElementById("grandTotalDisplay").innerHTML=ttlcost.toFixed(2);
    
                        document.getElementById("forfeitTable").style.display="none";
    
                          // this is just for testing to show which method was actually used 
                          if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                            // this is in its own if statement so it doesnt popuop while entering data
                            alert('if 2.3 was used');
                          }                  
                     }
              }
              else if (selectVal == "residualM" ) {
    
                  var diff = final_orTtl - final_nwTtl;
                  var displayDiff = diff* -1;
                  document.getElementById("differenceDisplay").innerHTML= displayDiff.toFixed(2);
                  frm.difference.value = displayDiff.toFixed(2);
    
                  if (diff > fee) {   
    
    
    
                      var mco =  diff - fee ;
                      document.getElementById("MCOInfo").innerHTML=mco.toFixed(2);
                      frm.MCOamt.value = mco.toFixed(2);
                      //document.getElementById("differenceDisplay").innerHTML=diff;
                      //frm.difference.value = diff* -1;
    
    
                      frm.totalcost.value = 0;
    
                      document.getElementById("ppDisplay").innerHTML=0;
                      document.getElementById("grandTotalDisplay").innerHTML=0;
    
                         if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                                  document.getElementById("forfeitTable").style.display="none";
                                  document.getElementById("MCOtable").style.display="table";
                              }
    
                                      // this is just for testing to show which method was actually used 
                          if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                            // this is in its own if statement so it doesnt popuop while entering data
                            alert('if 2.4 was used');
                          }
                  }
                  else {   
    
    
    
                      var remBal =  fee - diff ;
    
                        var ttlcost = remBal * gsts;
                        frm.totalcost.value = ttlcost.toFixed(2);
    
                        document.getElementById("ppDisplay").innerHTML=remBal.toFixed(2);
                        document.getElementById("grandTotalDisplay").innerHTML=ttlcost.toFixed(2);
    
                        document.getElementById("forfeitTable").style.display="none";
                        document.getElementById("MCOtable").style.display="none";
                                              // this is just for testing to show which method was actually used 
                          if (orBase != "" && orTax != "" && nwBase != "" && nwTax != "" && fee != "" && gsts != "") {
                            // this is in its own if statement so it doesnt popuop while entering data
                            alert('if 2.5 was used');
                          }
                  }
    
    
    
    
              }
        }
    

1 个答案:

答案 0 :(得分:0)

var nwTtl = fltrd_nwTtl / 100;
var final_orTtl = orTtl.toFixed(2)

var orTtl = fltrd_orTtl / 100;
var final_orTtl = orTtl.toFixed(2)


if (final_orTtl > final_nwTtl )  {

}

这部分似乎是我的问题。我在这里做的比较似乎并不像我之前因为某些原因而做的'.toFixed(2)'。我改成了:

var nwTtl = fltrd_nwTtl / 100;
var orTtl = fltrd_orTtl / 100;

if (orTtl > nwTtl )  {

}

我还在测试所有其他功能,但到目前为止,这似乎正如预期的那样。我仍然想知道为什么我使用'.toFixed(2)'导致两个明显高于低等级的数字来评估相反的情况。并将接受对此问题该部分的任何答复