繁体   English   中英

JavaScript在PHP while循环内将2个字段的答案乘以3中的答案

[英]Javascript multiply of 2 fields with answer in 3rd inside a PHP while loop

我正在显示来自MySQL表中使用的零件的数量和价格列表中的记录,并将所有更改更新回数据库,但我还需要具有一个计算所得的字段,该数量为*价格=数量。 我只是不能完全正常工作。 我是JavaScript新手。 我使用了GetElementById,它在我考虑的第一个总数中显示了NaN,我认为它可能遍历整个表格。

因此,在while循环中尝试创建动态ID,但无法显示它。 我不确定我是否在不提交页面的情况下正确实现计算。 任何帮助,将不胜感激,谢谢。

<?php
while ($list_parts = mysqli_fetch_array($parts_result))
{
    echo " <table border ='0' table width = 95%>";
    echo '<td> <input type="hidden" value="'.$list_parts['unique_id'].'" name="changed_unique_id[]">';
    echo '<td> <input type="hidden" value="'.$list_parts['job_number'].'" name="changed_job_number[]">';
    echo '<td> Quantity   <input type="text"  value="'.$list_parts['qty'].'" name="changed_part_quantity[]" id="qty'.$list_parts['unique_id'].'" onkeypress="qty'.$list_parts['unique_id'].'">';
    echo '<td> Description <input type="text" value="'.$list_parts['item_description'].'" name="changed_item_description[]">';
    echo '<td> Part Number <input type="text" value="'.$list_parts['part_number'].'" name="changed_part_number[]">';
    echo '<td> Part Price <input type="text"  value="'.$list_parts['price'].'" name="changed_part_price[]" id="price'.$list_parts['unique_id'].'" onkeypress="price'.$list_parts['unique_id'].'">';
    echo '<td> Total <input type="text" value="'.$list_parts['sub_total'].'" name="changed_part_sub[]" id="total'.$list_parts['unique_id'].'" >';
    echo "</tr>";   
}
echo "</table>";        
?>

我的JavaScript如下

function showtotal(){
    var quantity = document.getElementById("qty'.$list_parts['unique_id'].'").value;
    var price = document.getElementById("price'.$list_parts['unique_id'].'").value;
    var sum = parseInt(quantity) + parseInt(price);
    document.getElementById("total'.$list_parts['unique_id'].'").value = sum;
}

如果要在JavaScript代码中打印内容,则应插入php标签,以提高代码的可读性和安全性。 但是问题是,您试图将“ qty”和“ $ list_parts”的结果添加为“。”,这就是PHP代码,在JavaScript中,您使用“ +”。

我还注意到,在您的PHP代码中,您在while内创建了一个'table'元素,但仅在外部将其关闭。 这样,您的代码肯定会出现错误,因为将打开多个“表”,但只有一个关闭。 您应该将'table'的闭包移动到while循环内,或者将打开的内容移到while循环外。

编辑 -在getElementById里面,我写了“ qty +”,但是那是错误的,因为您创建的ID仅是“ qty”,因此JavaScript试图到达一个不存在的ID。

因此,您找到的JavaScript应该是:

function showtotal(){
    var quantity = document.getElementById("qty<?php echo $list_parts['unique_id']; ?>").value;
    var price = document.getElementById("price<?php echo $list_parts['unique_id']; ?>").value;
    var sum = parseInt(quantity) + parseInt(price);
    document.getElementById("total<?php echo $list_parts['unique_id']; ?>").value = sum;
}

非常感谢Ted的帮助。 您的帖子通过向我展示它的原理来帮助我解决了它。 之后,它不会让我在JavaScript中使用php。 我尝试了一种稍微不同的方法。 我不太确定这是否是正式的方式,但仍然很高兴。

        while ($list_parts = mysqli_fetch_array($parts_result))
            {
                echo "<table border ='0' table width = 95%>";
                echo '<td> <input type="hidden" value="'.$list_parts['unique_id'].'" name="changed_unique_id[]">';
                echo '<td> <input type="hidden" value="'.$list_parts['job_number'].'" name="changed_job_number[]">';
                echo '<td> Quantity   <input type="text" onchange="showtotal('.$list_parts['unique_id'].')" value="'.$list_parts['qty'].'" name="changed_part_quantity[]" id="qty'.$list_parts['unique_id'].'">';
                echo '<td> Description <input type="text" value="'.$list_parts['item_description'].'" name="changed_item_description[]">';
                echo '<td> Part Number <input type="text" value="'.$list_parts['part_number'].'" name="changed_part_number[]">';
                echo '<td> Part Price <input type="text" onchange="showtotal('.$list_parts['unique_id'].')" value="'.$list_parts['price'].'" name="changed_part_price[]" id="price'.$list_parts['unique_id'].'" >';
                echo '<td> Total <input type="text" id="total'.$list_parts['unique_id'].'" value="'.$list_parts['sub_total'].'" name="changed_part_sub[]">';
                echo "</tr>";
                echo $list_parts['unique_id'];
                echo'(total'.$list_parts['unique_id'].')';  
                echo "</table>";
            }

            ?>

Java脚本

function showtotal(id){
var quantity = document.getElementById('qty'+id).value;
var price = document.getElementById('price'+id).value;
var sum = parseInt(quantity,10)*parseInt(price,10); 
document.getElementById('total'+id).value = sum;

}

非常感谢你

罗布·格兰

暂无
暂无

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

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