计算选择框值

发布于 2024-10-30 18:58:51 字数 5366 浏览 1 评论 0原文

我想计算表单中的选择框值。例如:商品价格 * 数量

如果我只有一种计算形式,它就可以正常工作:

function calculate(){
    document.calcform.total.value = 
        (document.calcform.Chassis.value)*(document.calcform.Quantity.value)
}

但是,当我有多个计算时,它会给我 NaN.00:

function calculate(){
    document.calcform.total.value = 
        (document.calcform.Motherboard.value)
       *(document.calcform.Quantity.value)
       +(document.calcform.Chassis.value)
       *(document.calcform.Quantity1.value)
}

任何人都可以帮我解决它吗?我很感激。 我还在网页上发布了一张关于它给我的结果的图片。

这是我的代码:

<html>
<head>
<title></title>    
<style type = "text/css">
.SelectClass { 
  font-size: 15px; 
} 

table {

font-size: .9em;
border: 3px groove;
padding: 0px;
background-color: #dddddd;
}

.mycss
{
font-weight:normal;font-style:italic;color:#000000;letter-spacing:1pt;word-spacing:2pt;font-size:36px;text-align:left;font-family:courier new, courier, monospace;line-height:1;
}
</style>
<script LANGUAGE="JavaScript">

function dp(price)
{
string = "" + price;
number = string.length - string.indexOf('.');
if (string.indexOf('.') == -1)
return string + '.00';
if (number == 1)
return string + '00';
if (number == 2)
return string + '0';
if (number > 3)
return string.substring(0,string.length-number+3);
return string;
}

function calculate(){
document.calcform.total.value = dp((document.calcform.Motherboard.value)*(document.calcform.Quantity.value)+(document.calcform.Chassis.value)*(document.calcform.Quantity1.value))
}
</script>
</head>

<body onload="calculate">
<div class="mycss"><center>Order List</center></div><br />

<center>
<form name="calcform"  method="post" action="test2.php">
<table border="0"  width="800">
<tr>
<td align="right" colspan="2">
<font size="6">$</font><input name="total" style="border:0px solid #000000; background-color:#E6FBF4; font-size: 25.5px;"  type="text" value="0.00" ONCHANGE="calculate()"/> 
</td></tr>
<tr>
<td><font size="+1">
Product Name</font>
</td>
<td><font size="+1">Quantity</font></td></tr>
<td colspan="2">
<?php
mysql_connect("host", "user", "pass") or die(mysql_error());

mysql_select_db("a4202648_wang") or die(mysql_error());

echo "<table  width='100%'>";
echo "<tr><td align='left' width='90%'>";

$result = mysql_query("SELECT Motherboard_Part_Number, Motherboard_Name, Motherboard_Price FROM Motherboard where Motherboard_Part_Number='MB-435ED-3'") 
or die(mysql_error());  
echo '<select id="Motherboard" class="SelectClass" name="Motherboard" ONCHANGE="calculate()">'; 
// 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="Motherboard" value= ',$row['Motherboard_Price'],'>',$row['Motherboard_Part_Number'],'&nbsp;&nbsp;&nbsp;',$row['Motherboard_Name'],' &nbsp;&nbsp;&nbsp;$',$row['Motherboard_Price'],'&nbsp;','</option>'; 

} 

echo '</select>';
echo "</td>";
echo "<td align='right' width='10%'>";

$result= mysql_query("SELECT Number FROM Quantity") 
or die(mysql_error());  
echo '<select id="Quantity" class="SelectClass" name="Quantity" Value="Quantity" ONCHANGE="calculate()" >'; 

// 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 value=',$row['Number'],'>',$row['Number'],'</option>'; 
} 

echo '</select>';
echo "</td></tr>";
echo "<tr><td align='left' width='90%'>";

$result = mysql_query("SELECT Chassis_Part_Number, Chassis_Name, Chassis_Price FROM Chassis where Chassis_Form_Factor='ATX'") 
or die(mysql_error());  
echo '<select id="Chassis" class="SelectClass" name="Chassis" ONCHANGE="calculate()">'; 
// 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" value= ',$row['Chassis_Price'],'>',$row['Chassis_Part_Number'],'&nbsp;&nbsp;&nbsp;',$row['Chassis_Name'],' &nbsp;&nbsp;&nbsp;$',$row['Chassis_Price'],'&nbsp;','</option>'; 

} 

echo '</select>';
echo "</td>";
echo "<td align='right' width='10%'>";

$result= mysql_query("SELECT Number FROM Quantity") 
or die(mysql_error());  
echo '<select id="Quantity" class="SelectClass" name="Quantity1" Value="Quantity" ONCHANGE="calculate()" >'; 

// 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 value=',$row['Number'],'>',$row['Number'],'</option>'; 
} 

echo '</select>';
echo "</td></tr>";
echo "</table>";
?>
</td>
</tr>
<tr><td colspan="2"><input type="submit" /></td></tr>
</table>

</form>
</center>

</body>
</html>

I want to calculate selectbox value in a form. For example: item price * Quantity.

It works fine if I only have one calculation in the form:

function calculate(){
    document.calcform.total.value = 
        (document.calcform.Chassis.value)*(document.calcform.Quantity.value)
}

However, when I have more than one calculation, it gives me NaN.00:

function calculate(){
    document.calcform.total.value = 
        (document.calcform.Motherboard.value)
       *(document.calcform.Quantity.value)
       +(document.calcform.Chassis.value)
       *(document.calcform.Quantity1.value)
}

Anyone can help me to fix it? I appreciate it.
I also post a picture about the result that it gives me on the web page.

here is my code:

<html>
<head>
<title></title>    
<style type = "text/css">
.SelectClass { 
  font-size: 15px; 
} 

table {

font-size: .9em;
border: 3px groove;
padding: 0px;
background-color: #dddddd;
}

.mycss
{
font-weight:normal;font-style:italic;color:#000000;letter-spacing:1pt;word-spacing:2pt;font-size:36px;text-align:left;font-family:courier new, courier, monospace;line-height:1;
}
</style>
<script LANGUAGE="JavaScript">

function dp(price)
{
string = "" + price;
number = string.length - string.indexOf('.');
if (string.indexOf('.') == -1)
return string + '.00';
if (number == 1)
return string + '00';
if (number == 2)
return string + '0';
if (number > 3)
return string.substring(0,string.length-number+3);
return string;
}

function calculate(){
document.calcform.total.value = dp((document.calcform.Motherboard.value)*(document.calcform.Quantity.value)+(document.calcform.Chassis.value)*(document.calcform.Quantity1.value))
}
</script>
</head>

<body onload="calculate">
<div class="mycss"><center>Order List</center></div><br />

<center>
<form name="calcform"  method="post" action="test2.php">
<table border="0"  width="800">
<tr>
<td align="right" colspan="2">
<font size="6">
lt;/font><input name="total" style="border:0px solid #000000; background-color:#E6FBF4; font-size: 25.5px;"  type="text" value="0.00" ONCHANGE="calculate()"/> 
</td></tr>
<tr>
<td><font size="+1">
Product Name</font>
</td>
<td><font size="+1">Quantity</font></td></tr>
<td colspan="2">
<?php
mysql_connect("host", "user", "pass") or die(mysql_error());

mysql_select_db("a4202648_wang") or die(mysql_error());

echo "<table  width='100%'>";
echo "<tr><td align='left' width='90%'>";

$result = mysql_query("SELECT Motherboard_Part_Number, Motherboard_Name, Motherboard_Price FROM Motherboard where Motherboard_Part_Number='MB-435ED-3'") 
or die(mysql_error());  
echo '<select id="Motherboard" class="SelectClass" name="Motherboard" ONCHANGE="calculate()">'; 
// 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="Motherboard" value= ',$row['Motherboard_Price'],'>',$row['Motherboard_Part_Number'],'   ',$row['Motherboard_Name'],'    
,$row['Motherboard_Price'],' ','</option>'; 

} 

echo '</select>';
echo "</td>";
echo "<td align='right' width='10%'>";

$result= mysql_query("SELECT Number FROM Quantity") 
or die(mysql_error());  
echo '<select id="Quantity" class="SelectClass" name="Quantity" Value="Quantity" ONCHANGE="calculate()" >'; 

// 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 value=',$row['Number'],'>',$row['Number'],'</option>'; 
} 

echo '</select>';
echo "</td></tr>";
echo "<tr><td align='left' width='90%'>";

$result = mysql_query("SELECT Chassis_Part_Number, Chassis_Name, Chassis_Price FROM Chassis where Chassis_Form_Factor='ATX'") 
or die(mysql_error());  
echo '<select id="Chassis" class="SelectClass" name="Chassis" ONCHANGE="calculate()">'; 
// 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" value= ',$row['Chassis_Price'],'>',$row['Chassis_Part_Number'],'   ',$row['Chassis_Name'],'    
,$row['Chassis_Price'],' ','</option>'; 

} 

echo '</select>';
echo "</td>";
echo "<td align='right' width='10%'>";

$result= mysql_query("SELECT Number FROM Quantity") 
or die(mysql_error());  
echo '<select id="Quantity" class="SelectClass" name="Quantity1" Value="Quantity" ONCHANGE="calculate()" >'; 

// 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 value=',$row['Number'],'>',$row['Number'],'</option>'; 
} 

echo '</select>';
echo "</td></tr>";
echo "</table>";
?>
</td>
</tr>
<tr><td colspan="2"><input type="submit" /></td></tr>
</table>

</form>
</center>

</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2024-11-06 18:58:51

您正在尝试对字符串进行数学运算。 请尝试:或 parseInt 。

function calculate(){
    document.calcform.total.value = 
         parseFloat(document.calcform.Motherboard.value || 0)
       * parseFloat(document.calcform.Quantity.value    || 0)
       + parseFloat(document.calcform.Chassis.value     || 0)
       * parseFloat(document.calcform.Quantity1.value   || 0)
}

如果不需要浮点数,

You're trying to do math on strings. try:

function calculate(){
    document.calcform.total.value = 
         parseFloat(document.calcform.Motherboard.value || 0)
       * parseFloat(document.calcform.Quantity.value    || 0)
       + parseFloat(document.calcform.Chassis.value     || 0)
       * parseFloat(document.calcform.Quantity1.value   || 0)
}

or parseInt if you don't need floats.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文