好的我需要一些帮助,我有一个在线计算器,我必须从两个输入字段获得直线计.我需要它在输入数据时是自动的.我不知道如何做到这一点..任何编码帮助将不胜感激!这是html:
<input type="text" name="ancho" />
<input type="text" name="alto" />
<input type="text" name="material" />
Run Code Online (Sandbox Code Playgroud)
我需要它输出到这个字段:
<input type="text" name="ml" />
Run Code Online (Sandbox Code Playgroud)
这是数学方面的等式:(材料/ 100)/((alto/100)*(ancho/100))
提前感谢您的帮助!
叹息我在解决方案中没有看到很多错误检查,所以我想我还会发布一个 ......
// locate all the input fields on the page
$(':input')
// bind to anything change related (down to keyboard changes so the element
// won't need to lose focus, or the user won't have to press enter)
.bind('keypress keydown keyup change',function(){
// retrieve the values of the inputs (we also call parseFloat to confirm
// we are dealing with numeric values)
var acho = parseFloat($(':input[name="acho"]').val(),10),
alto = parseFloat($(':input[name="alto"]').val(),10),
matl = parseFloat($(':input[name="material"]').val(),10);
// default the end result to an empty string (you'll see
// why with the following statement)
var v = '';
// confirm that all three values that go in to the equation are
// all numbers before we try to perform any math functions. If
// all goes well, "v" above will have the actual resulting value.
// if any number is invalid, the "Result" field (ml) gets emptied
if (!isNaN(acho) && !isNaN(alto) && !isNaN(matl)){
// your math function
v = (matl / 100) / ((alto / 100) * (acho / 100));
}
// replace the value of "ml" with our new calculated value
$(':input[name="ml"]').val(v.toString());
});
Run Code Online (Sandbox Code Playgroud)
...... 有一个演示