JavaScript实时计算 [英] JavaScript Real Time Calculation

查看:128
本文介绍了JavaScript实时计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jeditable 构建了一个带有自定义输入数字的表格。一旦你输入值,输入类型就消失了

I have built a table with custom inputs numbers with jeditable. The Input type is gone once you put the value

我需要找到一个自动生成我的值的JavaScript实时计算。

I need to find a JavaScript Real Time Calculation which automatically makes the amount of my values.

我找到了两个非常适合我的案例的有趣例子,但是有可能在不使用表格和输入的情况下实现它吗?

I have found 2 interesting examples very suitable for my case but there is the possibility to achieve it the same without using the form and inputs?

第一个例子

第二示例

推荐答案

是的,确实如此。如您所知,可以通过 document.getElementById('div_id')访问div元素,并且可以通过 document.getElementById('div_id)访问其值)。价值

Yes, it is. As you know a div element can be accessed by document.getElementById('div_id') and its value can be accessed by document.getElementById('div_id').value.

所以取出表格并为 div id $ c>你需要的并访问该值然后找到总和,然后将值设置为总和到另一个 div 。这是代码

So take out the form and insert an id for the div's that you need and access the value and then find the sum and then set the value as the sum to another div. Here is the code

<script>
   function calculateBMI() {

   var wtStr =document.getElementById('w').value;

   if (!wtStr)
     wtStr = '0';

   var htStr = document.getElementById('h').value;
   if (!htStr)
     htStr = '0';


   var weight = parseFloat(wtStr);
   var height = parseFloat(htStr);
   document.getElementById("r").value = weight + height;
 }
 </script>

 <input id = "w" type="Text"  name="weight" size="4" onkeyup="calculateBMI()"> Weight  (in Kilos)  
 <input id = "h" type="Text" name="height" size="4" onkeyup="calculateBMI()"> Height (in Centimeters)<br>
 <input id = "r" type="Text" name="BodyMassIndex" id="BodyMassIndex" size="4"> BMI     
 <input type="button" style="font-size: 8pt" value="Calculate" onClick="calculateBMI()" name="button">

如果你不想要输入你可以使用 textarea

​and if you don't want input you can use textarea.

这篇关于JavaScript实时计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆