【问题标题】:how can i make my calculation using math.sqrt right?如何使用 math.sqrt 进行计算?
【发布时间】:2016-03-17 05:48:52
【问题描述】:

我只想得到 total2 的平方根 .. 但它不会出现在选定的框中 .. 这是javascript代码。 我会评论html代码。

function myFunction() {
  var q1 = document.getElementById("qinput1").value;
  var q2 = document.getElementById("qinput2").value;
  var q3 = document.getElementById("qinput3").value;
  var total = parseInt(q1) + parseInt(q2) + parseInt(q3);

  document.getElementById("ainput3").value=total;

  var a1 = document.getElementById("ainput1").value;
  var a2 = document.getElementById("ainput2").value;
  //from the total we got, lets assign it a variable for further calculation
  var a3 = document.getElementById("ainput3").value=total;
  var total2 = parseInt(a1)*parseInt(a2)/ parseInt(a3);
  document.getElementById("ansA").value = total2;

  var total3 = math.sqrt(parseInt(total2));
  document.getElementById("sqaureD").value = total3;
}

function myShapes() {
  document.getElementById('squareA').style.display = 
    document.getElementById('shapes').value == 'Square' ? 'block' : 'none'
}
<form action="" id="fcalculation"> 
  <fieldset> 
    <legend>Calculation of qu</legend> 
    <label><i>Ultimate bearing capacity</i> <b>(qu) = </b></label> 
    <input id="qinput1" type="text" placeholder="c'NcFcsFcdFci"/> + 
    <input id="qinput2" type="text" placeholder="qNqFqsFqdFqi"/> + 
    <input id="qinput3" type="text" placeholder="½βγNFγsFγdFγi"/> 
  </fieldset> 
</form>

似乎最后的计算部分不起作用。抱歉,这是我第一次编写代码。

【问题讨论】:

  • qu计算 + +
  • 控制台是否出现任何错误?

标签: javascript html variables math addition


【解决方案1】:

类名是 Math 而不是 math

尝试替换

var total3 = math.sqrt(parseInt(total2,10));

var total3 = Math.sqrt(parseInt(total2,10));

另外,查看您的标记,没有 ID 为 ainput1ainput2ainput3 的字段。

【讨论】:

  • 请将基数添加到parseInt
  • @NinaScholz 进行了更改,通常默认为 10
  • "如果输入字符串以“0”开头,则基数为 8(八进制)或 10(十进制)。具体选择哪个基数取决于实现。ECMAScript 5 指定 10 (使用十进制),但并非所有浏览器都支持这一点。因此,在使用 parseInt 时始终指定一个基数。"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
相关资源
最近更新 更多