使用jquery自动用数学更新输入字段?

luv*_*ode 4 forms jquery

好的我需要一些帮助,我有一个在线计算器,我必须从两个输入字段获得直线计.我需要它在输入数据时是自动的.我不知道如何做到这一点..任何编码帮助将不胜感激!这是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))

提前感谢您的帮助!

Bra*_*tie 5

叹息我在解决方案中没有看到很多错误检查,所以我想我还会发布一个 ......

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

...... 有一个演示

  • 有没有人读过"isNaN(acho)"作为"不是一个纳乔"?因为我完全认为应该是一个内置函数. (6认同)
  • @ mblase75:现在你提到它了.... @Other Answers:我可以问一下用代码回答问题的重点是什么?SO不是"谁能首先解决这个问题"网站,它是一个问答网站.浮现在脑海中的"教人钓鱼".令人失望的是看到这么多解决方案没有任何解释支持它们,这对OP(和未来的访问者)来说可能比解决方案本身更有帮助.`</咆哮>` (3认同)