使用javascript的简单数学不会改变表单中的字段

时间:2012-03-29 14:09:27

标签: javascript

我不能让我的表格的这部分工作。这是一个两阶段的形式。第二部分是相同的,它工作正常,它只是没有那么长

function calcTotal1()
        {
            var qty1, qty2, qty3, qty4, qty5, qty6, qty7, qty8, qty9, qty10, qty11, qty12, qty13;
            var ocost1, ocost2, ocost3, ocost4, ocost5, ocost6, ocost7, ocost8, ocost9, ocost10, ocost11, ocost12, ocost13;
            var otot1, otot2, otot3, otot4, otot5, otot6, otot7, otot8, otot9, otot10, otot11, otot12, otot13;
            var oSubTot6, oSubTot9;
            var oGrandTot;

            qty1 = document.orderform.sWorkbook8qty.value * 1.00;
            qty2 = document.orderform.sAnswerbook8qty.value * 1.00;
            qty3 = document.orderform.sWorkbook9qty.value * 1.00;
            qty4 = document.orderform.sAnswerbook9qty.value * 1.00;
            qty5 = document.orderform.sWorkbook10qty.value * 1.00;
            qty6 = document.orderform.sAnswerbook10qty.value * 1.00;
            qty7 = document.orderform.sStudentAnswerbook10qty.value * 1.00;
            qty8 = document.orderform.sWorkbook11qty.value * 1.00;
            qty9 = document.orderform.sAnswerbook11qty.value * 1.00;
            qty10 = document.orderform.sStudentAnswerbook11qty.value * 1.00;
            qty11 = document.orderform.sWorkbook12qty.value * 1.00;
            qty12 = document.orderform.sAnswerbook12qty.value * 1.00;
            qty13 = document.orderform.sStudentAnswerbook12qty.value * 1.00;


            ocost1 = document.orderform.sWorkbook8cost.value * 1.00;
            ocost2 = document.orderform.sAnswerbook8cost.value * 1.00;
            ocost3 = document.orderform.sWorkbook9cost.value * 1.00;
            ocost4 = document.orderform.sAnswerbook9cost.value * 1.00;
            ocost5 = document.orderform.sWorkbook10cost.value * 1.00;
            ocost6 = document.orderform.sAnswerbook10cost.value * 1.00;
            ocost7 = document.orderform.sStudentAnswerbook10cost.value * 1.00;
            ocost8 = document.orderform.sWorkbook11cost.value * 1.00;
            ocost9 = document.orderform.sAnswerbook11cost.value * 1.00;
            ocost10 = document.orderform.sStudentAnswerbook11cost.value * 1.00;
            ocost11 = document.orderform.sWorkbook12hgcost.value * 1.00;
            ocost12 = document.orderform.sAnswerbook12hgcost.value * 1.00;
            ocost13 = document.orderform.sStudentAnswerbook12hgcost.value * 1.00;


            otot1 = (eval(ocost1 * qty1)) * 1.00;
            otot2 = (eval(ocost2 * qty2)) * 1.00;
            otot3 = (eval(ocost3 * qty3)) * 1.00;
            otot4 = (eval(ocost4 * qty4)) * 1.00;
            otot5 = (eval(ocost5 * qty5)) * 1.00;
            otot6 = (eval(ocost6 * qty6)) * 1.00;
            otot7 = (eval(ocost7 * qty7)) * 1.00;
            otot8 = (eval(ocost8 * qty8)) * 1.00;
            otot9 = (eval(ocost9 * qty9)) * 1.00;
            otot10 = (eval(ocost10 * qty10)) * 1.00;
            otot11 = (eval(ocost11 * qty11)) * 1.00;
            otot12 = (eval(ocost12 * qty12)) * 1.00;
            otot13 = (eval(ocost13 * qty13)) * 1.00;

            document.orderform.sWorkbook8ttl.value = otot1;
            document.orderform.sAnswerbook8ttl.value = otot2;
            document.orderform.sWorkbook9ttl.value = otot3;
            document.orderform.sAnswerbook9ttl.value = otot4;
            document.orderform.sWorkbook10ttl.value = otot5;
            document.orderform.sAnswerbook10ttl.value = otot6;
            document.orderform.sStudentAnswerbook10ttl.value = otot7;
            document.orderform.sWorkbook11ttl.value = otot8;
            document.orderform.sAnswerbook11ttl.value = otot9;
            document.orderform.sStudentAnswerbook11ttl.value = otot10;
            document.orderform.sWorkbook12hgttl.value = otot11;
            document.orderform.sAnswerbook12hgttl.value = otot12;
            document.orderform.sStudentAnswerbook12hgttl.value = otot13;


            oSubTot6 = (oTot1 + oTot2) + (oTot3 + oTot4) + (oTot5 + oTot6) + (oTot7 + oTot8) + (oTot9 + oTot10) + (oTot11 + oTot12 + oTot13); 

            document.orderform.sSubtotal1.value = oSubTot6

            document.orderform.sGrandTotal.value = oSubTot6;
            }

1 个答案:

答案 0 :(得分:3)

我不确定什么不起作用,但只是重写代码可能会有所帮助:

  • 将名称放在数组中。
  • 循环显示名称并计算每对字段的总数。
  • 使用parseIntparseFloat将字符串转换为数字,而不是* 1.00
  • 您对eval的使用毫无意义,根本不需要。

示例:

var names = [
  'sWorkbook8','sAnswerbook8','sWorkbook9','sAnswerbook9',
  'sWorkbook10','sAnswerbook10','sStudentAnswerbook10',
  'sWorkbook11','sAnswerbook11','sStudentAnswerbook11',
  'sWorkbook12','sAnswerbook12','sStudentAnswerbook12'
];

var grand = 0;

for (var i = 0; i < names.length; i++) {
  var qty = parseInt(document.orderform[name[i] + 'qty'].value, 10);
  var cost = parseFloat(document.orderform[name[i] + 'cost'].value);
  var total = qty * cost;
  document.orderform[name[i] + 'ttl'].value = total;
  grand += total;
}

document.orderform.sSubtotal1.value = grand;
document.orderform.sGrandTotal.value = grand;