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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (45)

我正在尝试使用HTML和Javascript的小项目。我已经编写了3个函数,想要调用它们来显示一个结果,使用2个值在表单中输入。

我似乎在获得Javascript代码识别的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>
提问于
用户回答回答于

必须得到元素的值,就像这样。

//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>

扫码关注云+社区

领取腾讯云代金券