By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,672 Members | 1,644 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,672 IT Pros & Developers. It's quick & easy.

help needed with form selectedIndex property

P: n/a
I cannot get this code to work. Very simple...I have three list
menus. I want to do a check before the form submits to make sure that
the value of the 3 fields is equal to 12.
Here's my code...

<script language="javascript">
<!--
function addListValues() {
var grandtotal = 0;
var drop1Value,drop2Value,drop3Value;

var drop1Value=document.cakeForm.intQuant1.options[document.cakeForm.intQuant1.options.selectedIndex].value;
var drop2Value=document.cakeForm.intQuant2.options[document.cakeForm.intQuant2.options.selectedIndex].value;
var drop3Value=document.cakeForm.intQuant3.options[document.cakeForm.intQuant3.options.selectedIndex].value;

grandtotal = (drop1Value-0) + (drop2Value-0) + (drop3Value-0);

if (grandtotal < 12) {
alert( "You do not have enough items selected. You need to choose
12 cheesecakes." );
cakeForm.intQuant1.focus();
document.cakeForm.intQuant1.style.background = "#DEAA0C";
return false ;
}

if (grandtotal > 12) {
alert( "You have too many items selected. You need to choose 12
cheesecakes." );
cakeForm.intQuant1.focus();
document.cakeForm.intQuant1.style.background = "#DEAA0C";
return false ;
}

if (grandtotal = 12) {
document.cakeForm.submit();
//return false ;
}
}
//-->
</script>
Here is one of my three list menus:

<select name="intQuant1">
<option value="0">0</option>
<option value="12">12</option>
<option value="4">6</option>
<option value="4">4</option>
</select>

My submit buttonhas this call... onClick="return addListValues();

It does not appear to be working for all three list menus.

Many Thanks
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Lee
% =joe % said:

I cannot get this code to work. Very simple...I have three list
menus. I want to do a check before the form submits to make sure that
the value of the 3 fields is equal to 12.
Here's my code...

<script language="javascript">
The <script> tag should have a type attribute:
<script type="text/javascript">
<!--
That line does nothing. Remove it and the matching //-->
function addListValues() {
You could simplify the code, and make it more portable, by passing
arguments into it, instead of making the function know where to
find the values.

var grandtotal = 0;
var drop1Value,drop2Value,drop3Value;
Since you redefine each of these just below, these lines
don't really add anything (if you add a "var" to your
assignment to grandtotal, below).
var
drop1Value=document.cakeForm.intQuant1.options[document.cakeForm.intQuant1.options.selectedIndex].value;
var
drop2Value=document.cakeForm.intQuant2.options[document.cakeForm.intQuant2.options.selectedIndex].value;
var
drop3Value=document.cakeForm.intQuant3.options[document.cakeForm.intQuant3.options.selectedIndex].value;

grandtotal = (drop1Value-0) + (drop2Value-0) + (drop3Value-0);

if (grandtotal < 12) {
alert( "You do not have enough items selected. You need to choose
12 cheesecakes." );
cakeForm.intQuant1.focus();
That should be document.cakeForm... document.cakeForm.intQuant1.style.background = "#DEAA0C";
return false ;
}

if (grandtotal > 12) {
alert( "You have too many items selected. You need to choose 12
cheesecakes." );
cakeForm.intQuant1.focus();
Again, document.cakeForm...
document.cakeForm.intQuant1.style.background = "#DEAA0C";
return false ;
}

if (grandtotal = 12) {
The equality comparison operator is "==", not "=".
document.cakeForm.submit();
It's better to avoid submit() via Javascript. If you validate
in the onSubmit handler, then returning false will prevent the
form from submitting, and returning anything else will allow
it to submit.
//return false ;
}
}
//-->
Again, these HTML comments are not needed.
There are no longer any browsers in use that need them.
If there were, they wouldn't be able to use your form, anyway.
</script>
Here is one of my three list menus:

<select name="intQuant1">
<option value="0">0</option>
<option value="12">12</option>
<option value="4">6</option>
The value should be "6" in that line.

<option value="4">4</option>
</select>

My submit buttonhas this call... onClick="return addListValues();


Should be <form .... onsubmit="return addListValues(...)">

Here's a working example. Don't turn it in as your own homework:

<html>
<head>
<title>Say Cheesecake!</title>
<script type="text/javascript">
function addListValues(q1,q2,q3) {
var total = +q1.options[q1.selectedIndex].value
+ +q2.options[q2.selectedIndex].value
+ +q3.options[q3.selectedIndex].value;
if(total==12) {
return true;
} else {
alert("You have chosen " + total + " cheesecakes.\n"
+"You must choose exactly 12.");
return false;
}
}
</script>
</head>
<body>
<!-- The following line may wrap when posted -->
<form onsubmit="return
addListValues(this.intQuant1,this.intQuant2,this.i ntQuant3)">
<select name="intQuant1">
<option value="0">0</option>
<option value="12">12</option>
<option value="6">6</option>
<option value="4">4</option>
</select>
<select name="intQuant2">
<option value="0">0</option>
<option value="12">12</option>
<option value="6">6</option>
<option value="4">4</option>
</select>
<select name="intQuant3">
<option value="0">0</option>
<option value="12">12</option>
<option value="6">6</option>
<option value="4">4</option>
</select>
<input type="submit" value="submit">
</form>
</body>
</html>

Jul 23 '05 #2

P: n/a


Thanks. I appreciate the insight.

If you could point me in one direction to 'sharpen my javascript'
skills, where woul dit be?

Thanks again

joe

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 23 '05 #3

P: n/a
Lee
Joseph Weiss said:

Thanks. I appreciate the insight.

If you could point me in one direction to 'sharpen my javascript'
skills, where woul dit be?


This newsgroup.

Read questions, try to solve the problems by looking
things up on the web or in this newsgroups FAQ or the
resources it points to:

http://www.jibbering.com/faq/

or just by trying things that seem reasonable.
If you can solve the problem, post your answer.
If it's wrong or can be significantly improved,
somebody will let you know.

Either way, read other people's answers.

It really works.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.