将html中的表单值分配给javascript变量

时间:2018-10-18 20:35:49

标签: javascript html forms variables

我正在尝试一个使用HTML和Javascript的小项目。 我已经编程了3个函数,并想调用它们以使用以表格形式输入的2个值来显示结果。

我似乎为获得Java代码可识别的2个值而绊脚石。

如果有人能指出我做错了什么,我会很高兴?

这是我的项目...

//degrees to radians function
function degRad(deg) {
  var rad = ( Math.PI / 180) * deg;
  return rad;
}

//easting calcualtion function
function east(deg, dist) {
  var east = (Math.cos(degRad(deg)) * dist );
  return east;
}

// northing calculation functions
function north(deg, dist) {
  var north = (Math.sin(degRad(deg)) * dist);
  return north;
}

//calling functions and out putting the results
document.write("easting equals  " + east(document.getElementById("Ang"),document.getElementById("Dist")) + "<br>");

document.write("northing equals  " + north(document.getElementById("Ang"),document.getElementById("Dist")));
<!--This is the page elements to input the angle and distance-->

  <fieldset style="padding: 20px;">
	   <legend>Polar to Rectangular calculator</legend>
	    Angle: <input type="number" value="14" id="Ang" style="border: 1px solid;"/>
      Distance: <input type="number" value="10" id="Dist" style="border: 1px solid;"/>
      <input type="submit" value="Submit">
  </fieldset>

1 个答案:

答案 0 :(得分:1)

您必须像这样获取元素的值。

//degrees to radians function
function degRad(deg)
  {
  var rad = ( Math.PI / 180) * deg;
  return rad;
  }

//easting calcualtion function
function east(deg,dist)
  {
  var east = (Math.cos(degRad(deg)) * dist );
  return east;
  }

// northing calculation functions
function north(deg,dist)
  {
    var north = (Math.sin(degRad(deg)) * dist);
    return north;
  }

//CHANGES BELOW

//calling functions and out putting the results
document.write("easting equals  " + east(document.getElementById("Ang").value,document.getElementById("Dist").value) + "<br>");

document.write("northing equals  " + north(document.getElementById("Ang").value,document.getElementById("Dist").value));
<!--This is the page elements to input the angle and distance-->

  <fieldset style="padding: 20px;">
	   <legend>Polar to Rectangular calculator</legend>
	    Angle: <input type="number" value="14" id="Ang" style="border: 1px solid;"/>
      Distance: <input type="number" value="10" id="Dist" style="border: 1px solid;"/>
      <input type="submit" value="Submit">
  </fieldset>