在Javascript函数中嵌入Javascript函数

时间:2012-08-13 19:07:55

标签: javascript function

我需要一个函数来确定:(1)如果两个输入有值,则抛出一个错误。 (2)如果两个输入都没有值,则抛出错误。 (3)如果输入B或输入D有一个值,则确定使用哪个Calculate函数。当调用

时,两个Calculate函数都有效
onClick="Calculate(this.form.input_A.value, this.form.input_B.value, this.form.input_C.value, this.form)"
onClick="Calculate2(this.form.input_A.value, this.form.input_C.value, this.form.input_D.value, this.form)"

但是当我用来调用下面的函数时:

onclick="compute();"

错误有效,但我没有正确调用计算函数,我确定它显而易见但我看不到它。答案不再显示,就像添加错误功能之前一样。

<SCRIPT LANGUAGE="JavaScript">

function compute() {
  var B = document.getElementById("input_B").value;
  var D = document.getElementById("input_D").value;

  if (B != "" && D != "") {
    alert("You may only enter Assessment or Annual Property Taxes.");
  } else if (B != "") {
    Calculate(B);
  } else if (D != "") {
    Calculate2(D);
  } else {
    alert("You must enter a value into either Assessment or Annual Property Taxes.");
  }
}

function Calculate(Atext, Btext, Ctext, form) {
  var A = parseFloat(Atext);
  var B = parseFloat(Btext);
  var C = parseFloat(Ctext);
  form.Answer.value = ((B - A) * C) / 1000;
}

function Calculate2(Atext, Ctext, Dtext, form) {
  var A = parseFloat(Atext);
  var C = parseFloat(Ctext);
  var D = parseFloat(Dtext);
  form.Answer.value = D - ((A * C) / 1000);
}

function ClearForm(form) {
  form.input_A.value = "";
  form.input_B.value = "";
  form.input_C.value = "";
  form.input_D.value = "";
  form.Answer.value = "";
}

</SCRIPT>

2 个答案:

答案 0 :(得分:1)

您缺少参数而没有正确调用函数。 请尝试以下方法......

function compute() {   
    var B = document.getElementById("input_B").value;   
    var D = document.getElementById("input_D").value;    
    var A = document.getElementById("input_A").value;   
    var C = document.getElementById("input_C").value; 

    if (B != "" && D != "") {     
        alert("You may only enter Assessment or Annual Property Taxes.");   
    } else if (B != "") {     
        Calculate(A, B, C, this.forms[0]);   
    } else if (D != "") {     
        Calculate2(A, C, D, this.forms[0]);   } 
    else {     
        alert("You must enter a value into either Assessment or Annual Property Taxes.");   
    } 
}

答案 1 :(得分:0)

如果您的compute()函数只调用了一个参数调用了Calculate()Calculate2(),那么您需要创建只需要一个参数的新函数。

function CalculateB(Btext) {
  var B = parseFloat(Btext);
  var form = document.getElementById("docForm");
  form.Answer.value = B / 1000;
}

function CalculateD(Dtext) {
  var D = parseFloat(Dtext);
  var form = document.getElementById("docForm");
  form.Answer.value = D - (1 / 1000);
}