Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.
-
fshin #1
Sum Array
Hi,
I have a series of text boxes that are generated in a form based on a query.
They have the same name but have a digit attached to the end (text1, text2,
text 3). I need to sum these in another textbox (sum2). I am doing this with
Javascript on blur for each text box. I'm pretty new to javascript (VERY new).
Can someone look at this code for me? dmworkload is the query which generates
the number of textboxes.
<script language="javascript">
function AddEmUp(hrs)
{
<cfoutput>
var protarray = new Array(#dmworkload.recordcount#);
for (i = 0; i <= #dmworkload.recordcount#; i++)
{
protarray = hrs;
}
var sum;
sum=0;
for(i = 0; i <= #dmworkload.recordcount#; i++)
{
sum = sum + protarray;
}
document.workloader.total2.value = sum;
</cfoutput>
}
</script>
THanks,
Frank
fshin Guest
-
Converting an XML Array to a multi-level array
I have an array assigned to a data grid such as: private var myIngredients:Array = new Array( <item ln1="Plain" sn="plain" ln2="(3 cups)... -
[newbie]saving and reading array of associative array
i'm looking for examples of saving to file and reading back an array of associative array, in a ruby like way. saying i have something like : ... -
array data matches but array created in loop doesn't work
I have the exact same data in two arrays, but only the array created like so will work: $spaw_imglibs = array( array( 'value' =>... -
#24897 [Com]: array_multisort() will reindex the array but not if array length is 1
ID: 24897 Comment by: franklin_se at hotmail dot com Reported By: chro at sokrates dot uio dot no Status: ... -
#24897 [Opn->Asn]: array_multisort() will reindex the array but not if array length is 1
ID: 24897 Updated by: sniper@php.net Reported By: chro at sokrates dot uio dot no -Status: Open +Status: ... -
fshin #2
Re: Sum Array
OK, I've edited this a little bit.
---------
<script language="javascript">
function AddEmUp(hrs)
{
<cfoutput>
var protarray = new Array(#dmworkload.recordcount#);
for (var i = 1; i <= #dmworkload.recordcount#; i++)
{
protarray = hrs;
}
var sum;
sum=0;
for (var i = 1; i <= #dmworkload.recordcount#; i++)
{
sum = sum + parseInt(protarray);
}
document.workloader.total.value = sum;
</cfoutput>
}
</script>
-------------
The thing that doesnt work is whatever textbox is changed, that value is
assigned to each member of the array. Is there a way for the function to run
through each textbox instead of just the one actually calling the function?
Thanks,
Frank
fshin Guest
-
The ScareCrow #3
Re: Sum Array
No need for all the loops ect.
In the form field, just pass the value, just ensure that the intial value of
total2 or any other totals are zero.
<input type="text" name="text1" value="1" onblur="AddEmUp(this.value);">
<script language="javascript">
function AddEmUp(hrs)
{
var sum = parseInt(document.workloader.total2.value);
var addvalue = parseInt(hrs);
document.workloader.total2.value = sum + addvalue;
}
</script>
Ken
The ScareCrow Guest
-
fshin #4
Re: Sum Array
Hi Ken,
Thanks for your help! There is one remaining issue. If you change one of the
values in the textboxes and then change it again, the value is simply added to
the total rather than recalculating all of the boxes. For example, lets say
there are 3 boxes: 4, 5, and 1. The total2 box reads 10. If you change the 1
to 4, it reads 14 instead of 13. Any ideas?
Thanks,
Frank
fshin Guest
-
The ScareCrow #5
Re: Sum Array
Okay, this is a more involved senario. This will require a javascript function.
Basically when the onblur event occurs the js function is called. The js
function then loops through all the form fields. If the form feld name
contains the text "text" then add the value of this field to a varaible. After
the function has looped through all the form fields, then assign the variable
to the "total" form field.
The attached code is a complete working example of this.
Ken
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript">
<!--
function calculate(form){
var totalAmount = 0;
for(i=0; i < form.elements.length; i++){
var formField = form.elements[i].name;
if(formField.indexOf('text') != -1){
totalAmount = totalAmount + parseInt(form.elements[i].value);
}
}
form.total2.value = totalAmount;
}
//-->
</script>
</head>
<body>
<cfoutput>
<form>
<cfloop from="1" to="5" index="idx">
<input name="text#idx#" value="#idx#" onblur="calculate(this.form);">
</cfloop>
<input name="total2" value="0">
</form>
</cfoutput>
</body>
</html>
The ScareCrow Guest
-
fshin #6
Re: Sum Array
Scare Crow, Thanks so much! It is working except for one thing. For each
textbox created by the query there are that many textboxes made. That is, lets
say there are 4 textboxes made by the query results. Instead of only 4 boxes,
there are 16. Of if there should be 3, there are 9. Any thoughts?
fshin Guest
-
The ScareCrow #7
Re: Sum Array
I'm going to need to see some code
But if you are doing
<cfoutput query="queryname">
<input>
</cfoutput>
Then it should only create an input for each record of the query result set.
Ken
The ScareCrow Guest



Reply With Quote

