我用jeditable用自定义输入数字构建了一个表。输入值后,输入类型消失
我需要找到一个JavaScript实时计算,它可以自动计算我的值。
我发现2个有趣的示例非常适合我的情况,但是否有可能在不使用表格和输入的情况下实现相同的效果?
First example
Second example
最佳答案
是的。众所周知,document.getElementById('div_id')
可以访问div元素,而document.getElementById('div_id').value
可以访问其值。
因此,取出表格并为所需的id
插入div
并访问值,然后求和,然后将值设置为sum
到另一个div
。这是代码
<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">
如果您不想使用
input
,则可以使用textarea
。