更新另一个输入字段时更新输入字段

时间:2014-11-04 19:10:55

标签: javascript php forms

我无法让它发挥作用。

我有两种形式," mult"和" mult2"使用脚本进行一些简单的计算。这两个字段都依赖于另一种称为" recipe"。

的形式的数据

问题是,我实际上可以得到其中一种形式来完成它(就像我在这里看到的所有代码) - 奇怪的是它只有mult2可以工作并完成所有工作计算。 Mult1没有做任何事情。最终需要更新的字段是" gravity"和" gravity2" - 并且只有" gravity2"得到更新的计算结果。

我知道这里有一些很长的代码,但希望你能帮助我......

这是"配方"形式:

<form name="recipe">
<input type="text" size="1" maxlength="3" name="batchVal" value="30" onChange="calculate(malt1); calculate(malt2)"></td>
<input type="text" size="1" maxlength="3" name="efficiencyVal" value="75" onChange="calculate(malt1); calculate(malt2)"></td>
</form>

这是两种形式,&#34; mult&#34;和&#34; mult2&#34;两者都需要来自&#34; recipe&#34;的相同数据输出。形式:

<form name="mult">
      <select id="malt" name="malt" onChange="UpdateNextField(this,'ppg')">
      <option>Choose fermentable..</option>
      <?php

        $query = $mysqli->prepare("SELECT id,maltname,maltebc,maltppg FROM malt WHERE mainid = ?");

        $mainidcolumn = "1";

        /* bind parameters  */
        $query->bind_param("i", $mainidcolumn);
        $query->execute();

        $menu_result = $query->get_result();

        /* now you can fetch the results into an array */
        while ($menu = $menu_result->fetch_assoc()) {
        echo '<option value="'.$menu['maltppg'].'">' . $menu['maltname'] . ' ' . $menu['maltebc'] . ' EBC</option>';   

        }
        echo '</select>';
     ?> 

        <input type="text" id="ebcfield" size="1" maxlength="3">
    <input type="text" name="weightVal" size="1" maxlength="5" value="0" onChange="calculate(malt1)">
        <input name="grainpercent" type="text" id="grainpercent" size="1" maxlength="2">
        <input type="text" id="gravity" size="1" value="0" maxlength="4">
        <input name="graincolor" type="text" id="graincolor" size="1" maxlength="3">
        <input type="text" name="ppgVal" id="ppg" onMouseMove="calculate(malt1)">
    </form>

<form name="mult2">
      <select id="malt" name="malt" onChange="UpdateNextField(this,'ppg2')">
      <option>Choose fermentable..</option>
      <?php

        $query = $mysqli->prepare("SELECT id,maltname,maltebc,maltppg FROM malt WHERE mainid = ?");

        $mainidcolumn = "1";

        /* bind parameters  */
        $query->bind_param("i", $mainidcolumn);
        $query->execute();

        $menu_result = $query->get_result();

        /* now you can fetch the results into an array */
        while ($menu = $menu_result->fetch_assoc()) {
        echo '<option value="'.$menu['maltppg'].'">' . $menu['maltname'] . ' ' . $menu['maltebc'] . ' EBC</option>';   

        }
        echo '</select>';
     ?> 

        <input type="text" id="ebcfield" size="1" maxlength="3">
    <input type="text" name="weightVal2" size="1" maxlength="5" value="0" onChange="calculate(malt2)">
        <input name="grainpercent" type="text" id="grainpercent" size="1" maxlength="2">
        <input type="text" id="gravity2" size="1" value="0" maxlength="4">
        <input name="graincolor" type="text" id="graincolor" size="1" maxlength="3">
        <input type="text" name="ppgVal2" id="ppg2" onMouseMove="calculate(malt2)">
    </form>

最后是根据输入字段中的数据进行计算的javascript。计算(malt1)和计算(malt2)做同样的事情,它只是同一计算的两个实例。

<script type = "text/javascript">
function UpdateNextField(which,ppg) {
document.getElementById(ppg).value = which.value;
}

function UpdateNextField(which,ppg2) {
document.getElementById(ppg2).value = which.value;
}

</script>

<!-- calculations for malt 1-5 -->

<!-- form 1 - fermentable 1 -->
<script type="text/javascript">

    function calculate(malt1){
     var weightVal = document.mult.weightVal.value;
     var ppgVal = document.mult.ppgVal.value;
     var batchVal = document.recipe.batchVal.value;
     var efficiencyVal = document.recipe.efficiencyVal.value;
     var showValue = 0;

     var showValue = ((weightVal * ppgVal * 0.000008345) * (efficiencyVal) * 10) / batchVal + (1000);
     showValue = Math.round(showValue * 1) / 1;
     if (!isNaN(showValue)) {
                document.getElementById('gravity').value = showValue;
            }
    }


    function calculate(malt2){
     var weightVal2 = document.mult2.weightVal2.value;
     var ppgVal2 = document.mult2.ppgVal2.value;
     var batchVal = document.recipe.batchVal.value;
     var efficiencyVal = document.recipe.efficiencyVal.value;
     var showValue2 = 0;

     var showValue2 = ((weightVal2 * ppgVal2 * 0.000008345) * (efficiencyVal) * 10) / batchVal + (1000);
     showValue2 = Math.round(showValue2 * 1) / 1;
     if (!isNaN(showValue2)) {
                document.getElementById('gravity2').value = showValue2;
            }
    }
  </script>

1 个答案:

答案 0 :(得分:0)

Jay和LcLk指出,您的功能名称和重复ID存在一些问题。但是,假设您修复了这些问题,使用jQuery可以节省大量时间和精力。例如:

<强> HTML

<input type="text" id="first" />
<input type="text" id="second" />

<强>的Javascript

$("#first").keypress(function(){
    /* your custom code goes here */
    console.log("A key was pressed.");
});

这就是说,只要在选择第一个输入时按下某个键,它就会在控制台中打印出A key was pressed。然后,您可以直接从该匿名内部函数中调用计算函数,或直接将它们传递给keypress();