我被困在下拉框值计算中。有人可以帮我解决这个问题吗?
我的网页上有两个下拉框,下拉框中的值是从mysql数据库中恢复的。
下拉框A
显示两个不同的值,即Chassise_Name
和Chassise_price
,下拉框B
显示项目的数量。
Dropdown box "A" Dropdown box "B"
SuperServer 7036A-T(Black) $90 3
从Superserver 7036A-T(Black)
检索 Chassis_Name
;
从$90
检索chassis_Price
;
从其他表中的数量中检索3
。
问题:
如何才能仅在下拉框A
中将价格乘以下拉框B
中的数字?
这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head><title></title></head>
<body>
<form action="test2.php" method="post">
<?php
mysql_connect("host", "users", "pass") or die(mysql_error());
mysql_select_db("a4202648_wang") or die(mysql_error());
echo "<table border='1'>";
echo "<tr><td>";
$result = mysql_query("SELECT Chassis_Name, Chassis_Price FROM Chassis where Chassis_Form_Factor='ATX'")
or die(mysql_error());
echo '<select name="Chassis" onChange="change()">';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo '<option Name="Chassis">',$row['Chassis_Name'],' ',$row['Chassis_Price'],' ','</option>';
}
echo '</select>';
echo "</td>";
echo "<td>";
$result= mysql_query("SELECT Number FROM Quantity")
or die(mysql_error());
echo '<select name="Quantity" Value="Quantity" onChange="change()" >';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result)) {
// Print out the contents of each row into a table
echo '<option Name="Quantity">',$row['Number'],'</option>';
}
echo '</select>';
echo "</td></tr>";
echo "</table>";
?>
<input type="submit" />
</form>
</body>
</html>
答案 0 :(得分:0)
为了便于阅读,我减少了一些代码,但是你需要添加id =“chassis”和id =“quantity”,以便在进行求和时可以引用该dom对象。
务必将<script>
放在文档的头部。
<script type="text/javascript">
$('document').ready( function() {
function change() {
var quantity = $('#quantity').val();
var chassis = $('#chassis').val();
var sum = quantity * chassis;
$('#the_sum').val(sum);
}
});
</script>
<form action="test2.php" method="post">
<?php
echo '<select id="chassis" name="Chassis" onChange="change()">';
echo '<option value="{$row['Chassis_Name']}">{$row['Chassis_Name']} {$row['Chassis_Price']} </option>';
echo '<select id="quantity" name="Quantity" Value="Quantity" onChange="change()" >';
echo '<option value="{$row['Number']}">{$row['Number']}</option>';
?>
<input type="text" value="" id="the_sum" />
<input type="submit" />
</form>
</body>
</html>