当值改变javascript时调用函数

时间:2014-12-06 10:08:17

标签: javascript jquery html

嗨我想在值改变时调用函数

   <script>

function calculom(cantidad,promedio,inputtext,inputtextt){
    // Calculo del subtotal
    subtotal = promedio*cantidad;
    subtotall = promedio*cantidad;
    inputtext.value=subtotal;
    inputtextt.value=subtotall;

}

function calculos(cantidad,promedio,inputtext){

    // Calculo del subtotal
    subtotal = promedio+cantidad;
    inputtext.value=subtotal;
}

        </script>

和html

<div id="form-signin">
                    <input type="text" id="platanos" placeholder="Cantidad" onChange="calculom(this.value,precioplatanos.value,totalplatanos,totalplatanos4);">
                    <input type="text" id="precioplatanos" placeholder="Promedio" onChange="calculom(this.value,platanos.value,totalplatanos,totalplatanos4);"/> 
                    <input type="text" size="8" id="totalplatanos" name="tot" placeholder="Derp">
                    <hr>
                    <input type="text" id="platanos2" placeholder="Cantidad" onChange="calculom(this.value,precioplatanos2.value,totalplatanos2,totalplatanos5);">
                    <input type="text" id="precioplatanos2" placeholder="Promedio" onChange="calculom(this.value,platanos2.value,totalplatanos2,totalplatanos5);"/> 
                    <input type="text" size="8" id="totalplatanos2" name="tot2" placeholder="Derp" onclick="calculos(this.value,totalplatanos.value,totalplatanos3);">
                    <hr>
                    <input type="text" size="8" id="totalplatanos3" placeholder="Subtotal">

但它没有在这条线上工作:

<input type="text" size="8" id="totalplatanos2" name="tot2" placeholder="Derp" onchange="calculos(this.value,totalplatanos.value,totalplatanos3);">

至少如果我在该输入中手动更改,则值会发生变化 我想让它自动化

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要做的, 您的HTML代码应该是这样的,您应该使用onkeyup而不是onChange,而且您传递的ID不是totalplatanos5 totalplatanos4

<div id="form-signin">

<input type="text" id="platanos" placeholder="Cantidad" onkeyup="calculom(this.value,precioplatanos.value);">
<input type="text" id="precioplatanos" placeholder="Promedio" onkeyup="calculom(this.value,platanos.value,totalplatanos);"/> 
<input type="text" size="8" id="totalplatanos" name="tot" placeholder="Derp">
<hr>

<input type="text" id="platanos2" placeholder="Cantidad" onkeyup="calculom(this.value,precioplatanos2.value,totalplatanos2);">
<input type="text" id="precioplatanos2" placeholder="Promedio" onkeyup="calculos(this.value,platanos2.value,totalplatanos2,totalplatanos.value,totalplatanos3);"/> 
<input type="text" size="8" id="totalplatanos2" name="tot2" placeholder="Derp" />

<hr>

<input type="text" size="8" id="totalplatanos3" placeholder="Subtotal" />

</div>

您的JavaScript

    function calculom(cantidad,promedio,inputtext){
        // Calculo del subtotal
        subtotal = promedio*cantidad;
        inputtext.value=subtotal;
    }

    function calculos(cantidad,promedio,inputtext,drp1,total){
    subtotal = promedio*cantidad;
    derp2.value=subtotal;
    // Calculo del subtotal
  sum = Number(subtotal)+Number(derp1);
  total.value=sum;
    }

JSFIDDLE DEMO