|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Incremental Variables
I've got an ASP page where variables are added as the user inputs more data and they're named "Changed + i" where i is an incremental variable. So I have Changed1, Changed2, Changed3 and it continues. I want to run a for loop through these variables and check to see if the value of any of them equals another value. So I'm wondering how I can run a check on changed + i and how I refer to the name of the variable dynamically.
I forgot to mention this is in a javascript function on the page. |
|
#2
|
||||
|
||||
|
Can you post any code that you have so far?
You should simply be able to use a loop to loop through them, something like this (not tested): Code:
<script>
for(var a=1;a<=10;a++){
if(document.myform.elements["Changed"+a].value=="test"){
//condition is true!!
}
}
</script>
|
|
#3
|
|||
|
|||
|
Thank you, you just helped me put together this working script:
Code:
var i=0;
for (i=1;i<=25;i++)
{
if (document.frmUser.elements["CHANGED"+i].value=="T")
{
alert('error3');
if (document.frmUser.elements["Manage_Area_Name"+i].value == "XXX")
{
alert('error1');
}
else
{
alert('error2');
}
}
}
How do I find out how many times to go through the loop now? |
|
#4
|
||||
|
||||
|
Quote:
Sorry, I don't understand, how are you constructing the form? Will the number of textboxes change? |
|
#5
|
|||
|
|||
|
Yes, it's a list of items being pulled from a database and each one is given a name of CHANAGEDi where the variable i is an incremental number, different for each variable. So right now there's CHANGED1......CHANGED25 and if the user creates a new mamber in the database, there will be a CHANGED26 on the page as well. So I want to be able to run a single script that iwll run through all of them and do a comparison. The script I just posted works for right now but it's set to 25 loops, I was wondering how to get it to loop for the amount of CHANGEDi elements in the form.
Just to try and help a little more, I have a bunch of locations in the database and when the page loads it uses ASP/SQL to display each location on the page and named in a CHANGEDi sequence. If the user enters another location and clicks UPDATE or if they edit any of the existing locations and click UPDATE, I want to first do a check to make sure that location isn't already in the database before submitting the form. That's the long story. So if the user edits any of the fields, it does an onchange function to change the CHANGEDi value to "T". On an onsubmit, I want to cycle through all CHANGEDi elements and see if any of their values equal "T". If so, I want to get the value of the corresponding location and compare it to every element in the database to see if it's already in there. I can do a lot of it on my own but need help with a few things. |
|
#6
|
||||
|
||||
|
I will have to see your full code before I can help more, but if you are pulling the records from a database you could also increment a counter as you do it, and store this value somewhere on your form:
Code:
Dim numberofboxes
numberofboxes = 0
sql = "select * from yourtable"
'etc....
While NOT rs.EOF
'do something
numberofboxes = numberofboxes + 1
rs.Movenext
Wend
Response.Write("<input type=""text"" name=""howmany"" value=""" & numberofboxes & "">")
You could then use this value as the upper bound of your loop: Code:
var howmany = document.yourform.howmany.value;
for(var a=1;a<=howmany;a++){
//etc...
Last edited by sync_or_swim : May 7th, 2008 at 09:40 AM. |
|
#7
|
|||
|
|||
|
I just realized you may be waiting to see my code. Here's the part populating the locations onto the page:
Code:
<% If not rs_Users.EOF then rs_Users.movefirst Do While NOT rs_Users.EOF Count = Count + 1 If ColorOn = False then Response.Write "<TR Class=white>" For x = 0 to fldCount - 1 Response.Write "<TD Class=small><INPUT NAME=" & CHR(34) & fieldnames(x) & Count & CHR(34) & " ONCHANGE=vbscript:Document.frmUser.CHANGED" & Count & ".Value=" & CHR(34) & "T" & CHR(34) & " TYPE=TEXT CLASS=INPUTWHITE VALUE=" & CHR(34) & trim(rs_Users.Fields.item(x)) & CHR(34) & "></INPUT></TD>" & vbCRLF Next Response.Write "<TD><Input NAME=CHANGED" & Count & " CLASS=INPUTWHITE TYPE=HIDDEN SIZE=1 MAXLENGTH=1></INPUT></TD>" & vbCRLF & vbCRLF ColorOn = True Else Response.Write "<TR Class=blue>" For x = 0 to fldCount - 1 Response.Write "<TD Class=small><INPUT NAME=" & CHR(34) & fieldnames(x) & Count & CHR(34) & " ONCHANGE=vbscript:Document.frmUser.CHANGED" & Count & ".Value=" & CHR(34) & "T" & CHR(34) & " TYPE=TEXT CLASS=INPUTBLUE VALUE=" & CHR(34) & trim(rs_Users.Fields.item(x)) & CHR(34) & "></INPUT></TD>" & vbCRLF Next Response.Write "<TD><Input NAME=CHANGED" & Count & " CLASS=INPUTBLUE TYPE=HIDDEN SIZE=1 MAXLENGTH=1></INPUT></TD>" & vbCRLF & vbCRLF ColorOn = False End If Response.Write "</TR>" rs_Users.Movenext Loop End if %> |
|
#8
|
|||
|
|||
|
It displays it with either a white or blue background for easier reading (as usual) and the page source looks like so:
Code:
</TR><TR Class=blue><TD Class=small><INPUT NAME="Area_ID18" ONCHANGE=vbscript:Document.frmUser.CHANGED18.Value ="T" TYPE=TEXT CLASS=INPUTBLUE VALUE="AM5"></INPUT></TD> <TD Class=small><INPUT NAME="Area_Mgr18" ONCHANGE=vbscript:Document.frmUser.CHANGED18.Value ="T" TYPE=TEXT CLASS=INPUTBLUE VALUE="Norihisa Dobashi"></INPUT></TD> <TD Class=small><INPUT NAME="AREA_MGRJP18" ONCHANGE=vbscript:Document.frmUser.CHANGED18.Value ="T" TYPE=TEXT CLASS=INPUTBLUE VALUE="土橋律久"></INPUT></TD> <TD Class=small><INPUT NAME="Manage_Area_Name18" ONCHANGE=vbscript:Document.frmUser.CHANGED18.Value ="T" TYPE=TEXT CLASS=INPUTBLUE VALUE="Higashi Tokyo"></INPUT></TD> <TD Class=small><INPUT NAME="Regional_Mgr_ID18" ONCHANGE=vbscript:Document.frmUser.CHANGED18.Value ="T" TYPE=TEXT CLASS=INPUTBLUE VALUE="2"></INPUT></TD> <TD><Input NAME=CHANGED18 CLASS=INPUTBLUE TYPE=HIDDEN SIZE=1 MAXLENGTH=1></INPUT></TD> </TR><TR Class=white><TD Class=small><INPUT NAME="Area_ID19" ONCHANGE=vbscript:Document.frmUser.CHANGED19.Value ="T" TYPE=TEXT CLASS=INPUTWHITE VALUE="AM6"></INPUT></TD> <TD Class=small><INPUT NAME="Area_Mgr19" ONCHANGE=vbscript:Document.frmUser.CHANGED19.Value ="T" TYPE=TEXT CLASS=INPUTWHITE VALUE="Masako Tsuneki"></INPUT></TD> <TD Class=small><INPUT NAME="AREA_MGRJP19" ONCHANGE=vbscript:Document.frmUser.CHANGED19.Value ="T" TYPE=TEXT CLASS=INPUTWHITE VALUE="常木昌*"></INPUT></TD> <TD Class=small><INPUT NAME="Manage_Area_Name19" ONCHANGE=vbscript:Document.frmUser.CHANGED19.Value ="T" TYPE=TEXT CLASS=INPUTWHITE VALUE="Tokyo Mid"></INPUT></TD> <TD Class=small><INPUT NAME="Regional_Mgr_ID19" ONCHANGE=vbscript:Document.frmUser.CHANGED19.Value ="T" TYPE=TEXT CLASS=INPUTWHITE VALUE="2"></INPUT></TD> <TD><Input NAME=CHANGED19 CLASS=INPUTWHITE TYPE=HIDDEN SIZE=1 MAXLENGTH=1></INPUT></TD> That's numbers 18 and 19 as you can see, there's 25 in total. Does that help? |
|
#9
|
||||
|
||||
|
Hi,
Thanks for posting your code, it helps me to see exactly what is going on. You know the number of records because you are incrementing a counter so store this value on your form something like this: Code:
<%
If not rs_Users.EOF then
rs_Users.movefirst
Do While NOT rs_Users.EOF
Count = Count + 1
If ColorOn = False then
Response.Write "<TR Class=white>"
For x = 0 to fldCount - 1
Response.Write "<TD Class=small><INPUT NAME=" & CHR(34) & fieldnames(x) & Count & CHR(34) & " ONCHANGE=vbscript:Document.frmUser.CHANGED" & Count & ".Value=" & CHR(34) & "T" & CHR(34) & " TYPE=TEXT CLASS=INPUTWHITE VALUE=" & CHR(34) & trim(rs_Users.Fields.item(x)) & CHR(34) & "></INPUT></TD>" & vbCRLF
Next
Response.Write "<TD><Input NAME=CHANGED" & Count & " CLASS=INPUTWHITE TYPE=HIDDEN SIZE=1 MAXLENGTH=1></INPUT></TD>" & vbCRLF & vbCRLF
ColorOn = True
Else
Response.Write "<TR Class=blue>"
For x = 0 to fldCount - 1
Response.Write "<TD Class=small><INPUT NAME=" & CHR(34) & fieldnames(x) & Count & CHR(34) & " ONCHANGE=vbscript:Document.frmUser.CHANGED" & Count & ".Value=" & CHR(34) & "T" & CHR(34) & " TYPE=TEXT CLASS=INPUTBLUE VALUE=" & CHR(34) & trim(rs_Users.Fields.item(x)) & CHR(34) & "></INPUT></TD>" & vbCRLF
Next
Response.Write "<TD><Input NAME=CHANGED" & Count & " CLASS=INPUTBLUE TYPE=HIDDEN SIZE=1 MAXLENGTH=1></INPUT></TD>" & vbCRLF & vbCRLF
ColorOn = False
End If
Response.Write "</TR>"
rs_Users.Movenext
Loop
End if
Response.Write("<input type=""text"" name=""howmany"" value=""" & Count & "">")%>
Then use this value in your javascript loop (I can't see what you form is called, change the name accordingly): Code:
var howmany = document.yourform.howmany.value;
for(var a=1;a<=howmany;a++){
//etc...
|
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Incremental Variables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
![]() |
|