473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

help needed with form selectedIndex property

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
3 4986
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lee Mundie | last post by:
Hi there, Simple problem here but can't seem to fix it! Okay, I have a select list from which people choose avatars... the list is option values ie. <option>Worm</option> ...
6
by: James Walker | last post by:
Can some one help I get an error of 'checkIndate' is null or not an object can someone please help. I can't work out why Thanks in advance James <form> <td height="24" colspan="7"...
7
by: ???????J | last post by:
Javascript may inquire the push down menu value, can I inquire the description? The following example, the variable($answer) can be get the menu1's value. For example, if I select first data,...
1
by: Randi | last post by:
Hi, Thanks David and I got that to work, I guess I still need to get it to work using the arrays and the selectedIndex property. I have to use an alert box that shows the total price like: The...
10
by: dhnriverside | last post by:
Hi guys Still having a problem with this dropdownlist. Basically, I've got 4. The first 2 work fine, then my code crashes on the 3rd. ddlEndTimeHour.Items.FindByValue(endTime).Selected =...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
7
by: Rotsey | last post by:
Hi, I am loading a tab control on a form. The code loads textboxes and comboboxes and checkboxes, normal data entry form that loads a table row of data. I have a combo on the form above the...
18
by: Academia | last post by:
I let the use modify the text of a combobox and then I replace the selected item with the new text (in Keyup event). But if he sets the Text property to an empty string ("") that sets the...
106
by: bonneylake | last post by:
Hey Everyone, Well i don't know if my question should be in javascript/ajax or coldfusion, i figure this is more of a coldfusion question. But if this is in the wrong section let me know an all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.