I cannot seem to figure out where I went wrong with this.
favorite
share [g+] share [fb] share [tw]
I’m creating a website as a small store for personal use, my main question here is how would I code a function for the additon of all of it?
six single check boxes for various types of peripheral devices including printer, monitors, modems or other devices with which you are familiar. Assume the basic computer system price of $500.00 and then add appropriate prices based on user checks.
Monitor 299.99 Printer 129.99 Speakers 49.99 CDRW 159.99 Scanner 129.99 Modem 49.99
If I add just a scanner, my total will be $629.99. If I add a modem and a monitor, my total will be $849.98.
I may be overlooking something simple. When I click a checkbox, it does not add to the total like it’s supposed to.
<html>
<head>
<title>Problem 9</title>
<script type="text/javascript" src="code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(function() {
function calculateCost() {
var ret = 500;
var $selected = $('input:checked', $peripherals);
$selected.each(function() {
ret += parseFloat($(this).attr('alt'));
});
return ret.toFixed(2);
}
function setCost() {
$total.text(calculateCost());
}
var $peripherals = $('#peripherals');
var $total = $('#total');
$('input[type="checkbox"]', $peripherals).on('change', setCost);
setCost();
});
</script>
<body>
<h1>What peripherals do you want to add?</h1>
<form id="peripherals">
<input type="checkbox" name="Monitor" value="Monitor" alt="299.99">Monitor<br>
<input type="checkbox" name="Printer" value="Printer" alt="129.99">Printer<br>
<input type="checkbox" name="Speakers" value="Speakers" alt="49.99">Speakers<br>
<input type="checkbox" name="CDRW" value="CDRW" alt="159.99">CDRW<br>
<input type="checkbox" name="Scanner" value="Scanner" alt="129.99">Scanner<br>
<input type="checkbox" name="Modem" value="Modem" alt="49.99">Modem<br>
</form>
<p>Total cost:<span id="total">0</span></p>
</body>
</html>
Looking at the JavaScript console, I bet it would have an error about
$not being defined. It is because you are not linking to the jQuery js file correctly.It is looking for it on your domain, not jQuery’s
notice that I added the
//JSFiddle
If you are running this off the file system and not a server, you need to add
http://So it will use the right protocol to fetch the file or just download the file and link to it locally.