PDA

View Full Version : I cant seem to get the function to [email protected]@



theogn8219
Mar 23rd, 2007, 04:51 AM
Hi guys,
Been working on this for hours. Everything seem correct to me, but for some reason i just couldnt get it working correctly. The script im wrote is to calculate the total price of a product from a product selection list (quantity * product price) and then display the result. Any way, i suspect that the function i wrote is wrong. Could some one take a look please. THanks in advance.

<html>
<head>
<title>Assignment 2</title>
<script language='javascript'>

function calcTotal()
{
var quantityTotal = parseFloat(document.myForm.qinput.value);
var productNumber = parseFloat(document.myForm.productSelection.options[document.myForm.productSelection.selectedIndex].value);

switch (productNumber)
{
case "1":
document.myForm.product1.value = 2.98 * quantityTotal;
<br></br>
case "2":
document.myForm.product2.value = 4.50 * quantityTotal;
<br></br>
case "3":
document.myForm.product3.value = 9.98 * quantityTotal;
<br></br>
case "4":
document.myForm.product4.value = 4.49 * quantityTotal;
<br></br>
case "5":
document.myForm.product5.value = 6.87 * quantityTotal;
<br></br>
}
}
</script>
</head>
<body>
<form method = "get" name = "myForm">

<h1>Weekly Sales Form</h1>
<h2>Products Sold</h2>

<label>Product:</label>
<select name = "productSelection" id="productSelection">
<option value = "1" selected = "selected">Product 1 - $2.98</option>
<option value = "2">Product 2 - $4.50</option>
<option value = "3">Product 3 - $9.98</option>
<option value = "4">Product 4 - $4.49</option>
<option value = "5">Product 5 - $6.87</option>
</select>


<label>Quantity:</label>
<input name = "qinput" type="text" id="qinput" size="5" maxlength="5">
<input type="button" value="Enter" ONCLICK="calcTotal()">

<br></br>


<h2>Product Totals</h2>
<table border="1" width="235">
<tr>
<th>Product #</th>
<th>Total Sales</th>
</tr>
<tr>
<td>Product 1</td>
<td><input name="product1" type="text" value="$0.00" disabled></td>
</tr>
<tr>
<td>Product 2</td>
<td><input name="product2" type="text" value="$0.00" disabled></td>
</tr>
<tr>
<td>Product 3</td>
<td><input name="product3" type="text" value="$0.00" disabled></td>
</tr>
<tr>
<td>Product 4</td>
<td><input name="product4" type="text" value="$0.00" disabled></td>
</tr>
<tr>
<td>Product 5</td>
<td><input name="product5" type="text" value="$0.00" disabled></td>
</tr>

</table>
</form>
</body>
</html>

Kor
Mar 23rd, 2007, 07:35 AM
Welcome to the Forum!

You have set the swich() argument productNumber as a decimal number (with parseFloat()), but you use for case a string. And remove the HTML tags <br></br> from the javascript code:


switch (productNumber)
{
case 1:
document.myForm.product1.value = 2.98 * quantityTotal;
case 2:
document.myForm.product2.value = 4.50 * quantityTotal;
.......