在动态创建的表上执行函数
我正在使用 PHP 将 30 个烘焙项目从 mySQL 数据库检索到动态 HTML 表中。检索到的数据为产品、商品(主键)、重量和价格。我的代码还为每个项目创建一个名为数量的输入框,以便用户可以输入他想要的案例数。下面是用于生成动态表的代码段:
$i=0;
while ($i < $num) {
$Item=mysql_result($result,$i,"Item");
$Product=mysql_result($result,$i,"Product");
$Weight=mysql_result($result,$i,"Weight");
$BGPrice=mysql_result($result,$i,"BGPrice");
echo "<tr>";
echo "<td>$Product</td>";
echo "<td><INPUT NAME=Item size=5 value=$Item READONLY></td>";
echo "<td><INPUT NAME=Weight size=5 value=$Weight READONLY></td>";
echo "<td><INPUT NAME=Price size=5 value=$BGPrice READONLY></td>";
echo "<td><INPUT NAME=Quantity size=5 value=0 tabindex=$i></td>";
echo "<td><INPUT NAME=ExtPrice size=5 value=0 READONLY></td>";
echo "<td><INPUT NAME=TotalWt size=5 value=0 READONLY></td>";
echo "</tr>";
$i++;
}
我需要 JavaScript 来调用函数并计算扩展价格 (ExtPrice) 和总重量 (TotalWt) 的值)一旦用户输入他想要订购的箱数。
这是我的挣扎:这个动态表中有 30 个项目,每个项目的 INPUT NAME 都是相同的。如何创建一个函数来更新每个产品的 ExtPrice 和 TotalWt?
I am retrieving 30 bakery items from a mySQL database into a dynamic HTML table using PHP. The data retrieved are Product, Item (Primary Key), Weight, and Price. My code also creates an INPUT box, called Quantity, for each item so that the user can type in how many cases he wants. Below is a segment of the code used to generate the dynamic table:
$i=0;
while ($i < $num) {
$Item=mysql_result($result,$i,"Item");
$Product=mysql_result($result,$i,"Product");
$Weight=mysql_result($result,$i,"Weight");
$BGPrice=mysql_result($result,$i,"BGPrice");
echo "<tr>";
echo "<td>$Product</td>";
echo "<td><INPUT NAME=Item size=5 value=$Item READONLY></td>";
echo "<td><INPUT NAME=Weight size=5 value=$Weight READONLY></td>";
echo "<td><INPUT NAME=Price size=5 value=$BGPrice READONLY></td>";
echo "<td><INPUT NAME=Quantity size=5 value=0 tabindex=$i></td>";
echo "<td><INPUT NAME=ExtPrice size=5 value=0 READONLY></td>";
echo "<td><INPUT NAME=TotalWt size=5 value=0 READONLY></td>";
echo "</tr>";
$i++;
}
I need JavaScript to call a function and calculate the values for Extended Price (ExtPrice) and Total Weight (TotalWt) as soon as the user enters the number of cases he would like to order.
Here's my struggle: there are 30 items in this dynamic table and each item's INPUT NAME is the same. How can I create a function that updates ExtPrice and TotalWt for each individual product?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
$i
变量来唯一标识每个输入echo "";
并且作为旁注,使用引号或双引号将
$Item
括起来,因为它可能包含空格等。You could use the
$i
variable to uniquely identify each inputecho "<td><INPUT NAME=Item id='item$i' size=5 value='$Item' READONLY></td>";
And as a side note use quotes or double quotes to wrap
$Item
as it may contains spaces, etc..您的 JavaScript 不应直接按名称引用每个
输入
。相反,获取对同一行中相邻单元格的引用。假设您有一个重新计算结果的提交按钮,下面是一个示例:
这不会执行任何错误检查,并且可以通过多种方式进行改进。它只是为了说明一种方法。
请参阅工作示例。
Your JavaScript should not directly reference each
input
by name. Instead, obtain a reference to the adjacent cells in the same row.Assuming you have a submit button that re-calculates the results, here's an example:
This doesn't do any error checking and could be improved in several ways. It's just meant to illustrate one approach.
See a working example.