...

View Full Version : Select value always returns 0



mrwireful9000
08-21-2008, 04:07 PM
I have four select boxes and I want to add the values of the last three. For some reason, the values are always 0.
I've tested this in Firefox 3 and IE 7 and neither give me any errors, so I guess it's just grabbing the value before it's changed.
I would really appreciate any advice on how to fix this. Thanks in advance!



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>

<style type="text/css">
form.c1 {display:none}
form.c2 {display:inline}
</style>
<script type="text/javascript">
window.onload = function() {
document.seasonform.season.selectedIndex = '0';
document.lengthoform.lengtho.selectedIndex = '0';
document.lengthpform.lengthp.selectedIndex = '0';
document.petform.pet.selectedIndex = '0';
}
function showL() {
if (document.seasonform.season.selectedIndex == '0') {
document.lengthoform.style.display = "none";
document.lengthpform.style.display = "none";
document.lengthoform.lengtho.selectedIndex = '0';
document.lengthpform.lengthp.selectedIndex = '0';
document.petform.style.display = "none";
document.petform.pet.selectedIndex = '0';
}
if (document.seasonform.season.selectedIndex == '1') {
document.lengthoform.lengtho.selectedIndex = '0';
document.lengthoform.style.display = "none";
document.lengthpform.style.display = "inline";
}
if (document.seasonform.season.selectedIndex == '2') {
document.lengthpform.lengthp.selectedIndex = '0';
document.lengthpform.style.display = "none";
document.lengthoform.style.display = "inline";
}
}
function showPp() {
if (document.lengthpform.lengthp.selectedIndex == '0') {
document.petform.style.display = "none";
document.petform.pet.selectedIndex = '0';
}
if (document.lengthpform.lengthp.selectedIndex != '0') {
document.petform.style.display = "inline";
}
}
function showPo() {
if (document.lengthoform.lengtho.selectedIndex == '0') {
document.petform.style.display = "none";
document.petform.pet.selectedIndex = '0';
}
if (document.lengthoform.lengtho.selectedIndex != '0') {
document.petform.style.display = "inline";
}
}
function showPaypal() {
if (document.petform.pet.selectedindex = '0') {
document.paypal.style.display = "none";
}
if (document.petform.pet.selectedindex = '1') {
document.paypal.style.display = "inline";
}
if (document.petform.pet.selectedindex = '2') {
document.paypal.style.display = "inline";
}
}
function total() {
this.lp = document.lengthpform.lengthp.value;
this.lo = document.lengthoform.lengtho.value;
this.pet = document.petform.pet.value;
}
</script>
</head>
<body>
<script type="text/javascript">
</script>
<form name="seasonform" action="" class="c2">
<select name="season" onChange="showL();">
<option value="0">Select One</option>
<option value="peak">Peak Season</option>
<option value="off">Off Season</option>
</select>
</form>
<br>
<form name="lengthpform" action="" class="c1">
<select name="lengthp" onChange="showPp();">
<option value="0">Select One</option>
<option value="595">2 Days</option>
<option value="760">3 Days</option>
<option value="925">4 Days</option>
<option value="1080">5 Days</option>
<option value="1245">6 Days</option>
<option value="1400">1 Week</option>
</select>
</form>
<form name="lengthoform" action="" class="c1">
<select name="lengtho" onChange="showPo();">
<option value="0">Select One</option>
<option value="696">2 Days</option>
<option value="720">3 Days</option>
<option value="845">4 Days</option>
<option value="970">5 Days</option>
<option value="1095">6 Days</option>
<option value="1200">1 Week</option>
</select>
</form>
<br>
<form name="petform" action="" class="c1">
<select name="pet" onChange="showPaypal();">
<option value="0">Select One</option>
<option value="0">No</option>
<option value="100">Yes</option>
</select>
</form>
<form name="paypal" action="" class="c1">
<script type="text/javascript">
var V = new total();
document.write(V.lp);
document.write(V.lo);
document.write(V.pet);
// This is written so that I can see the variables. It does not need to be here when the problem is fixed.
</script>
</form>
</body>
</html>

chaosprime
08-21-2008, 04:45 PM
Your code is mostly fine. Your difficulty is just that you're thinking that the JS in this:
<form name="paypal" action="" class="c1">
<script type="text/javascript">
var V = new total();
document.write(V.lp);
document.write(V.lo);
document.write(V.pet);
// This is written so that I can see the variables. It does not need to be here when the problem is fixed.
</script>
</form>
is being executed when you display that block. It's not. It's executed when the page first loads, when all the values are 0. So when it displays, you're thinking it's telling you your values are 0, but it's not, it's telling you your values were 0 when the page first loaded. If you change that debugging output to a function and run it when you set the paypal block to display: inline, you'll be fine.

Philip M
08-21-2008, 04:49 PM
The function total() is never called except on window.onload()

rnd me
08-21-2008, 10:10 PM
the bug is here:


function showPaypal() {
if (document.petform.pet.selectedindex = '0') {
document.paypal.style.display = "none";
}
if (document.petform.pet.selectedindex = '1') {
document.paypal.style.display = "inline";
}
if (document.petform.pet.selectedindex = '2') {
document.paypal.style.display = "inline";
}
}

when you call:

if (document.petform.pet.selectedindex = '0')


you are actually assigning the selected index a value of 0, as you observed.

remember to use the comparison "==" operator, not the assignment "=" operator:

if ( document.petform.pet.selectedIndex == '0')

in fact, you really could simply use :


if (!document.petform.pet.selectedIndex )


hope this help you, i know how frustrating one-char bugs are!!!



EDIT: it's also "selectedIndex" not "selectedindex", you need to fix this to.

mrwireful9000
08-23-2008, 01:21 AM
Thanks everyone! I re-wrote it with a button, because I finally gave up on making it work without one. The only problem now is that when I add the values together, they add as a string, not as numbers. is there any way to fix this? I tried adding a " + 0" to the end, but that didn't help.

Here is my new code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>

<style type="text/css">
form.c1 {display:none}
form.c2 {display:inline}
</style>
<script type="text/javascript">
window.onload = function() {
document.seasonform.season.selectedIndex = '0';
document.lengthoform.lengtho.selectedIndex = '0';
document.lengthpform.lengthp.selectedIndex = '0';
document.petform.pet.selectedIndex = '0';
}
function total() {
amt = document.lengthpform.lengthp.value + document.lengthoform.lengtho.value + document.petform.pet.value;
}
function showL() {
if (document.seasonform.season.selectedIndex == '0') {
document.lengthoform.style.display = "none";
document.lengthpform.style.display = "none";
document.lengthoform.lengtho.selectedIndex = '0';
document.lengthpform.lengthp.selectedIndex = '0';
document.petform.style.display = "none";
document.petform.pet.selectedIndex = '0';
}
if (document.seasonform.season.selectedIndex == '1') {
document.lengthoform.lengtho.selectedIndex = '0';
document.lengthoform.style.display = "none";
document.lengthpform.style.display = "inline";
}
if (document.seasonform.season.selectedIndex == '2') {
document.lengthpform.lengthp.selectedIndex = '0';
document.lengthpform.style.display = "none";
document.lengthoform.style.display = "inline";
}
}
function showPp() {
if (document.lengthpform.lengthp.selectedIndex == '0') {
document.petform.style.display = "none";
document.petform.pet.selectedIndex = '0';
}
if (document.lengthpform.lengthp.selectedIndex != '0') {
document.petform.style.display = "inline";
}
}
function showPo() {
if (document.lengthoform.lengtho.selectedIndex == '0') {
document.petform.style.display = "none";
document.petform.pet.selectedIndex = '0';
}
if (document.lengthoform.lengtho.selectedIndex != '0') {
document.petform.style.display = "inline";
}
}
function showPaypal() {
if (document.petform.pet.selectedindex = '0') {
document.paypal.style.display = "none";
}
if (document.petform.pet.selectedindex = '1') {
document.paypal.style.display = "inline";
total();
}
if (document.petform.pet.selectedindex = '2') {
document.paypal.style.display = "inline";
total();
}
}
function button() {
document.write(amt);
}
</script>
</head>
<body>
<script type="text/javascript">
</script>
<form name="seasonform" action="" class="c2">
<select name="season" onChange="showL();">
<option value="0">Select One</option>
<option value="peak">Peak Season</option>
<option value="off">Off Season</option>
</select>
</form>
<br>
<form name="lengthpform" action="" class="c1">
<select name="lengthp" onChange="showPp();">
<option value="0">Select One</option>
<option value="595">2 Days</option>
<option value="760">3 Days</option>
<option value="925">4 Days</option>
<option value="1080">5 Days</option>
<option value="1245">6 Days</option>
<option value="1400">1 Week</option>
</select>
</form>
<form name="lengthoform" action="" class="c1">
<select name="lengtho" onChange="showPo();">
<option value="0">Select One</option>
<option value="696">2 Days</option>
<option value="720">3 Days</option>
<option value="845">4 Days</option>
<option value="970">5 Days</option>
<option value="1095">6 Days</option>
<option value="1200">1 Week</option>
</select>
</form>
<br>
<form name="petform" action="" class="c1">
<select name="pet" onChange="showPaypal();">
<option value="0">Select One</option>
<option value="0">No</option>
<option value="100">Yes</option>
</select>
</form>
<br>
<form name="paypal" action="" class="c1">
<input type="button" name="gen" value="Click here!" onClick="button();"></input>
<script type="text/javascript">

</script>
</form>
</body>
</html>

chaosprime
08-23-2008, 01:37 AM
You need to do parseInt(val1) + parseInt(val2) etc.

IrDewey
08-24-2008, 04:32 AM
You could also use " x -= -y", since subtracting a negative is the same as adding.

mrwireful9000
08-25-2008, 02:55 AM
Thanks guys!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum