简体   繁体   English

使用 onekeyup 脚本计算表单输入

[英]Calculate form inputs with onekeyup script

I'm looking for a solution for the following issue,我正在寻找以下问题的解决方案,

I need a Javascript code to calculate this with onekeyup.我需要一个 Javascript 代码来用 onekeyup 计算这个。 So for example, if I insert a value of 2 in text1.例如,如果我在 text1 中插入值 2。 In text2 the value has to be 200.在 text2 中,该值必须为 200。

      <form>

        <input type='text' name='text1'>

        <input type='text' name='text2'>

        <script>
        var factor= 100;
        var text1= value in text2 * factor;
        var text2= value in text1 / factor;

      </form>

quick and dirty:又快又脏:

<input id="text1" type='text' name='text1' onkeyup="document.getElementById('text2').value = parseInt (this.value) * 100;">
<input id="text2" type='text' name='text2'>

I've stored the empty variables outside the function, and then set the values within the function when the input value has changed.我将空变量存储在函数外部,然后在输入值更改时设置函数内的值。

 var factor= 100; var text1; var text2; document.querySelector('input[name="text2"]').onkeyup = function() { text1 = document.querySelector('input[name="text2"]').value * factor; console.log(text1); } document.querySelector('input[name="text1"]').onkeyup = function() { text2 = document.querySelector('input[name="text1"]').value / factor; console.log(text2) }
 <form> <input type='text' name='text1'> <input type='text' name='text2'> </form>

this is the answer.这就是答案。

The HTML HTML

<input type="text" id="valor1">
    <span id="resultado1"></span>
  <input type='text' id="valor2">
  <span id="resultado2"></span>

The JS JS

$(function()
{
    var factor = 100;
    $("#valor1").on("keyup",function()
    {
    var valor1 = parseInt($("#valor1").val());
        var resultado1 = valor1 * factor;
        $("#resultado1").text(resultado1);
    })
    $("#valor2").on("keyup",function()
    {
    var valor2 = parseInt($("#valor2").val());
        var resultado2 = valor2 / factor;
        $("#resultado2").text(resultado2);
    }) 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM