Javascript 在 PHP while 循环中将 2 个字段与第 3 个字段的答案相乘
Javascript multiply of 2 fields with answer in 3rd inside a PHP while loop
我正在显示来自 MySQL table 所用零件的数量和价目表的记录,并将任何更改更新回数据库,但我还需要一个计算字段,该数量* 价格 = 金额。
我似乎无法让它正常工作。我是 JavaScript 的新手。
我使用了 GetElementById,它在第一个总数中显示了一个 NaN,然后我想到了它,我认为它可能会贯穿整个 table.
因此尝试在 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 标签,以保证代码的可读性和安全性。但问题是您正在尝试 'add' 您的 "qty" 和带有 '.' 的“$list_parts”的结果,即 PHP 代码,在 JavaScript,你使用'+'。
我还注意到,在您的 PHP 代码中,您在 while 内部创建了一个 'table' 元素,但仅在外部将其关闭。您的代码肯定会出现错误,因为多个 'table' 将打开,但只有一个关闭。您应该将 'table' 的闭包移到 while 循环内,或将开环移到 while 循环外。
EDIT - 在 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 的帮助。您的 post 向我展示了它的原理,帮助我解决了这个问题。在它不允许我在 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>";
}
?>
Javascript
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;
}
非常感谢
罗伯·格雷恩
我正在显示来自 MySQL table 所用零件的数量和价目表的记录,并将任何更改更新回数据库,但我还需要一个计算字段,该数量* 价格 = 金额。 我似乎无法让它正常工作。我是 JavaScript 的新手。 我使用了 GetElementById,它在第一个总数中显示了一个 NaN,然后我想到了它,我认为它可能会贯穿整个 table.
因此尝试在 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 标签,以保证代码的可读性和安全性。但问题是您正在尝试 'add' 您的 "qty" 和带有 '.' 的“$list_parts”的结果,即 PHP 代码,在 JavaScript,你使用'+'。
我还注意到,在您的 PHP 代码中,您在 while 内部创建了一个 'table' 元素,但仅在外部将其关闭。您的代码肯定会出现错误,因为多个 'table' 将打开,但只有一个关闭。您应该将 'table' 的闭包移到 while 循环内,或将开环移到 while 循环外。
EDIT - 在 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 的帮助。您的 post 向我展示了它的原理,帮助我解决了这个问题。在它不允许我在 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>";
}
?>
Javascript
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;
}
非常感谢
罗伯·格雷恩