简体   繁体   English

Javascript-计算两个相关字段之间的余数

[英]Javascript - calculating the remaining between two related fields

First of all, I would like to calculate the remaining QTY (quantity) between two fields in a form; 首先,我想以表格的形式计算两个字段之间的剩余数量(数量); in other words I have the actual QTY input text field as a read-only where its value is obtained from mysql table AND there is the user QTY input text field is taken from the user input. 换句话说,我有一个实际的QTY输入文本字段为只读,其中它的值是从mysql表中获得的,并且用户QTY输入文本字段是从用户输入中获取的。 All I want is to decrement the actual QTY based on what the user input in the user QTY . 我只想根据用户在用户QTY中输入的内容减少实际QTY

I have the following code as follows : 我有以下代码如下:

$query2 = "SELECT * from store_00";
$result2 = mysql_query($query2);
$n = array();
while($row2 = mysql_fetch_assoc($result2)){
echo "<td align=center>".$row2['item_no']."</td><td align=center>";
?>
<input type="text" id="fname" value="<?php echo $row2['qty'] ?>" readonly="readonly"/>
<?php
echo "</td>";

for($i=0;$i<$num;$i++){
echo "<td align=center>"; ?><input autofocus="autofocus" onchange="myFunction(this.value);" type="text" name="n[]" value=""/><?php echo "</td>";
}
echo "</tr>";
}

And the Javascript code : 和Javascript代码:

function myFunction(v)
{
var x=document.getElementById("fname");
x.value = x.value - v;
}

The above will output in the actual QTY correctly for the first time, but when user changes the value back to 0 , it will decrements from the new actual QTY value which gives a false remaining...So there must be a way to have a temporary actual QTY value saved somewhere. 上面的将第一次正确输出实际的QTY ,但是当用户将值更改回0时,它将从新的实际QTY值中递减,这会导致剩余的假值...因此必须有一种方法临时实际的QTY值保存在某处。

Anyways I know the code looks awful, I'm still new to JS and not sure how to just create a simple relation and remaining calculation between two fields. 无论如何,我知道代码看起来很糟糕,但是我对JS还是陌生的,并且不确定如何在两个字段之间创建简单的关系和剩余的计算。

As you don't use jQuery and your question is about javascript, i've changed some things to do what you want easily. 由于您不使用jQuery,而您的问题是关于javascript的,因此我更改了一些方法来轻松实现您想要的功能。 Also, the id should be unique while classes don't need to be unique. 同样,id应该是唯一的,而类不必是唯一的。 As you have ids with the same values that creates a problem, also as you stated, you don't store your original value, and this is your main problem. 正如您所声明的那样,由于具有具有相同值的ID也会造成问题,因此您不会存储原始值,这是您的主要问题。 There are many methods for this but i prefer this as it's easy to understand i guess. 有很多方法可以实现,但是我更喜欢这样做,因为我猜很容易理解。

$n = array();
echo "<table>";
while($row2 = mysql_fetch_assoc($result2)){
    echo "<tr class='inputs'>";
    echo "<td align=center> ".$row2['item_no']." </td><td align=center>";
    echo "<input class='original' type='text' id='c".$row2['item_no']."' value='".$row2['qty']."' readonly='readonly'/>";
    echo "</td>";
    for($i=0;$i<$num;$i++){
        echo "<td align=center>";
        echo "<input class='d".$row2['item_no']."'  data-goto='c".$row2['item_no']."' data-original = '".$row2['qty']."' autofocus='autofocus' onchange='myFunction(this);' type='text' name='n[]' value='0'/>";
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table>";

With the html5, you can define custom tags with the data- prefix, and you can reach them with getAttribute method for that object. 使用html5,您可以定义带有data-前缀的自定义标签,并且可以使用该对象的getAttribute方法访问它们。

<script>  
    function myFunction(v){
        var minus = 0;
        var mutualInputs = document.getElementsByClassName(v.className);
        for (var i = 0; i < mutualInputs.length; ++i) {
            var item = mutualInputs[i];  
            minus = minus + parseInt(item.value);
        }
        var original = v.getAttribute('data-original');
        var x = document.getElementById(v.getAttribute('data-goto'));
        x.value = original - minus;
    }
</script>

ps: your item_no 's must be unique in this example. ps:您的item_no在此示例中必须是唯一的。

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

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