Javascript pythagoras函数显示为undefined

时间:2014-01-30 23:58:46

标签: javascript

我是初学者,不明白为什么以下显示为未定义。

以下是我的剧本:

 function pythagoras() {
     function solvepy(form) {
         var a = parseFloat(form.a.value);
         var b = parseFloat(form.b.value);
         c.value = Math.sqrt(a * a + b * b);
     }

     function pythagoras(form) {
         var aInput = document.getElementById("a");
         var bInput = document.getElementById("b");
     }
     var result = displayResult();
 }

 function displayResult(a, b, c) {
     var div = document.getElementById("result");
     div.innerHTML = "Triangle: a = " + a + ", b = " + b + ". c = " + c;
 }

以下是我的身体:

 <form>
  <label for="a">Enter lengths for sides a: </label>
  <input type="number" id="a" size="3">
  <label for="b"> and b: </label>
  <input type="number" id="b" size="3"><br>
  <input type="button" id="submit" value="Compute c!">
 </form>
 <div id="result">
 </div>

1 个答案:

答案 0 :(得分:0)

试试这个..

Javascript:

// Set global variables, so they can be accessed trough all functions
var a, b, c;

function calculate() {
         a = parseFloat(document.pythagora.a.value);
         b = parseFloat(document.pythagora.b.value);
         c = Math.sqrt(a * a + b * b);
         displayResult(a,b,c);
}
function displayResult(a, b, c) {
     var div = document.getElementById("result");
     div.innerHTML = "Triangle: a = " + a + ", b = " + b + ". c = " + c;
}

<form name="pythagora">
  <label for="a">Enter lengths for sides a: </label>
  <input type="number" id="a" size="3">
  <br>
  <label for="b"> and b: </label>
  <input type="number" id="b" size="3"><br>
  <br>
  <input type="button" id="submit" value="Compute c!" onClick="calculate();">
</form>
<div id="result">
</div>

Here is a working example link..(click here)