I am currently creating a form that requires me to have multiple comboboxes which have various options with different values.
What I am having a problem with is finding a javascript snippet that will allow me to add the total value of all the comboboxes based on the selections.
I have an editbox at the bottom of the form where I would like the total value to appear.
I finally figured out how to calculate the form total based on the checkboxes and radio selections, but I can't find any javascript for comboboxes.
I would appreciate any type of help I can get.
Thanks!
3 2387
Hi Htidirect,
Its simple to get the value for the selection box. But it has the complex of how you are going to add.
sample html - <select id='mySelect' onchange='addValue(this)'>
-
<option value='1'>One</option>
-
<option value='2'>Two</option>
-
<option value='3'>Three</option>
-
<option value='4'>Four</option>
-
<option value='5'>Five</option>
-
</select>
sample JS: -
-
function addValue(ths)
-
{
-
alert(ths.options[ths.selectedIndex].value);
-
}
This how you will be able to get the value from the select box. But onchange you have to minus the previous selection value and add the new value.
Thanks and Regards
Ramanan Kalirajan
- <script type="text/javascript" language="Javascript">
-
var sum=0;
-
price = document.frmOne.select1.value;
-
document.frmOne.txtDisplay.value = price;
-
function OnChange(value){
-
-
price = document.frmOne.select1.value;
-
quantity = document.frmOne.select2.value;
-
sum = price * quantity;
-
-
document.frmOne.txtDisplay.value = sum;
-
}
-
</script>
-
<form NAME = "frmOne" action="initiateorder.php" method="post">
-
Price:<br><INPUT name = "select1" TYPE = "Text" style="border:#999999 solid 1px; background-color:#FFF; width:40px; height:20px;" value ="2500" size = "35"><br>
-
Quantity:<br><select name="select2" onchange='OnChange(this.value)' style="border:#999999 solid 1px; background-color:#FFF; width:40px; height:20px;">
-
<option value="1">1</option>
-
<option value="2">2</option>
-
<option value="3">3</option>
-
<option value="4">4</option>
-
<option value="5">5</option>
-
<option value="6">6</option>
-
<option value="7">7</option>
-
<option value="8">8</option>
-
<option value="9">9</option>
-
<option>10</option>
-
</select><br>
-
Result:<br>
-
<INPUT TYPE = "Text" name = "txtDisplay" size = "35" value ="" style="border:#999999 solid 1px; background-color:#FFF; width:40px; height:20px;" readonly><br><br>
-
</form>
there you go "mckatuni"
I read the initial request differently.
I thought OP wanted to sum MULTIPLE select boxes (comboboxes)
to arive at a total.
Here is my attempt: -
<!DOCTYPE html>
-
<html lang="en">
-
<head>
-
<meta charset="utf-8" />
-
<title> </title>
-
<!-- For: http://bytes.com/topic/javascript/answers/885630-need-help-calculating-total-based-combobox-selections -->
-
-
<style type="text/css">
-
#debugger { height:200px; width:300px; background-color:orange; border:1px solid blue; }
-
</style>
-
-
</head>
-
<body>
-
<div id='debugger'>
-
<select id="SBox1" onchange="showOptions('colorPick',this)"></select> <input type="text" value="" id="colorPick"><p>
-
<select id="SBox2" onchange="showOptions('flavorPick',this)"></select> <input type="text" value="" id="flavorPick"><p>
-
<select id="SBox3" onchange="showOptions('fruitPick',this)"></select> <input type="text" value="" id="fruitPick"><p>
-
<select id="SBox4" onchange="showOptions('statePick',this)"></select> <input type="text" value="" id="statePick"><p>
-
</div>
-
<button onclick="calculate()">Summation</button> <input type="text" value="" id="summation" readonly>
-
-
<script type="text/javascript">
-
Array.prototype.SBox = function(IDS) {
-
var str = ''; var tarr = [];
-
for (var i=0; i<this.length; ++i) {
-
if (this[i].indexOf('~') != -1) { tarr = this[i].split('~'); }
-
else { tarr = [0,0]; tarr[0] = this[i]; tarr[1] = i; } // this[i]; } // default: selectedIndex | value
-
str += '<option value="'+tarr[1]+'">'+tarr[0]+'</option>';
-
} document.getElementById(IDS).innerHTML=str;
-
}
-
function calculate() {
-
var sum = 0;
-
var sel = document.getElementById('debugger').getElementsByTagName('select');
-
for (var i=0; i<sel.length; i++) {
-
sum += Number(sel[i].value);
-
}
-
document.getElementById('summation').value = sum;
-
}
-
function showOptions(ids,formInfo) {
-
var tmp = formInfo.value;
-
if (tmp != '~') { document.getElementById(ids).value = formInfo.value; }
-
else { document.getElementById(ids).value = ''; }
-
}
-
var Colors = ['Choose color~','1 Red~1','2 Orange~2','3 Yellow~3','4 Green~4','5 Blue~5','6 Indigo~6','7 Violet~7'];
-
var Flavors = ['Choose flavor~','1 Vanilla~1','2 Chocolate~2','3 Strawbery~3','4 Lime~4','5 Blueberry~5'];
-
var Fruits = ['Choose fruit','1 Apple~1','2 Bannana~2','3 Cantaloupe~3','4 Kiwi~4','5 Orange~5','6 Watermelon~6'];
-
var States = ['State~','1 Alabama~1','2 Alaska~2','3 Arkansas~3','7 Florida~7'];
-
-
window.onload = function () {
-
var str = '';
-
Colors.SBox('SBox1');
-
Flavors.SBox('SBox2');
-
Fruits.SBox('SBox3');
-
States.SBox('SBox4');
-
}
-
</script>
-
-
</body>
-
</html>
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Piet |
last post by:
Hello wxPythoneers.
I have a problem with a dialog box derived from wxFrame which has a
wxComboBox as main element. Depending on the entry selected from the
ComboBox, the dialog box will be...
|
by: Steve |
last post by:
I have an unbound combobox in the form header of a continuous form. The
selection in the combobox sets the where clause in a querydef which determines
QryPFrmInventoryManagement. The following code...
|
by: Luther |
last post by:
I want to create a form that searches a table. The hard part is this,
I'd like to have the available records filtered based on combobox
selections. For example, if this were a vehicle database, I...
|
by: Joe |
last post by:
I have 2 multi-list boxes, 1 displays course categories based on a
table called CATEGORIES. This table has 2 fields CATEGORY_ID,
CATEGORY_NAME
The other multi-list box displays courses based on...
|
by: my-wings |
last post by:
I think I've painted myself into a corner, and I'm hoping someone can help
me out.
I have a table of books (tblBooks), which includes a field (strPubName) for
Publisher Name and another field...
| |
by: Greg Bradburn |
last post by:
Greetings,
I have a form with two comboboxes that I want to be populated with the
same values from a table (i.e. both comboboxes have the same
table/column as their datasource and display...
|
by: Lars Netzel |
last post by:
Hello!
I have a number of files to copy. I have to total Amount of files and the
Total FileSize of this operation (for example 3453 files, 4065.4 Mb)... How
can I calculate how much time this...
|
by: jon f kaminsky |
last post by:
Hi-
I've seen this problem discussed a jillion times but I cannot seem to
implement any advice that makes it work. I am porting a large project from
VB6 to .NET. The issue is using the combo box...
|
by: pmarisole |
last post by:
I need help in calculating a score from a row of drop-down values.
I need to use the onChange to tally the score as the user moves across
9 categories (with drop-down selection of 1-9 or N/A)...
|
by: Lisa |
last post by:
In translating the formula for calculating lottery odds for various
conditions into a Visual Basic Program, I have apparently missed something
in that I get errors in the part of the calculation...
|
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,...
| |
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |