Hej, jag har två dropdownlistor. #2 av dessa uppdateras beroende på vad som väljs i #1. Detta görs ett ett javascript. Problem: När jag "post" till en annan sida så skickar jag inte den valda texten, utan det skickas Array index. Hur plockar jag ur texten från dropdownlistan. Kod finns nedan, "visa källa". Om det finns ett value på en option så skickas det som värde för kontrollen, annars skickas texten. Tack för förtydligande hur option och value fungerar, det hjälper mig en del. Men "1" och "2" måste vara med för att få listbox #2 beroende av listbox #1. #2 uppdateras inte om "value= 1" och "value= 2" tas bort i javascripten. Känner mig som en dumb nybörjare som inte får till det. :-) Du har inte bifogat koden för clickcombo() funktionen. Data som fyller Array kommer från en databas så "case" är ingen lösning. Här nedan är koden för scriptet som anropas. Scriptet är gjort för tre listboxar men jag har anpassat det till två.Skicka text, inte Array Index.
Sida Ett (med submit knapp):
<SELECT NAME="fieldname1" SIZE="1" ONCHANGE="return(clickcombo(1,document.forms[0].fieldname1,document.forms[0].fieldname2,document.forms[0].fieldname3));">
<OPTION VALUE=1>Applikationsdrift</OPTION><OPTION VALUE=2>Server</OPTION></SELECT>
<SELECT NAME="fieldname2" SIZE="1" ONCHANGE="return(clickcombo(2,document.forms[0].fieldname1,document.forms[0].fieldname2,document.forms[0].fieldname3));"><OPTION>Inget valt</OPTION></SELECT>
<SCRIPT LANGUAGE="JavaScript">
var array1 = new Array();
var array2 = new Array();
array1[1] = new Array(
1001,"Applikationsdrift s/390",
1002,"Applikationsdrift AS/400");
array1[2] = new Array(
2001,"Tekniskdrift S/390",
2002,"Backup");
// values for Applikationsdrift s/390
array2[1001] = new Array(
);
// values for Applikationsdrift AS/400
array2[1002] = new Array(
);
// values for Tekniskdrift S/390
array2[2001] = new Array(
);
// values for Backup
array2[2002] = new Array(
);
</SCRIPT>
********************
Sida två (Skriver ut skickad data i textarea):
<TD class="liten">
<div align="right">
<input type="text" name="txtomrade2" Value= <%Response.write(request.form("fieldname1"))%> size="25" maxlength="25">
</div>
</TD>
<TD class="liten">
<div align="right">
<input type="text" name="txtomrade2" Value= <%Response.write(request.form("fieldname2"))%> size="25" maxlength="25">
</div>
</TD>Returvärde för listval
En option som ser ut så här: <option value=1>Applikation</option> ger värdet 1
En option som ser ut så här: <option>Applikation</option> ger värdet ApplikationSv: Skicka text, inte Array Index.
Här skapas och fylls listboxarna, och ettan i "(clickcombo(1," måste vara där:
***********
sSelect = "<SELECT NAME=""" & sFormFieldName & "1"" SIZE=""" & nSize & _
""" ONCHANGE=""return(clickcombo(1,document.forms[0]." & _
sFormFieldName & "1,document.forms[0]." & sFormFieldName & _
"2,document.forms[0]." & sFormFieldName & "3));"">" & vbCrlf
***********
""" ONCHANGE=""return(clickcombo(2,document.forms[0]." & sFormFieldName & _
"1,document.forms[0]." & sFormFieldName & "2,document.forms[0]." & _
sFormFieldName & "3));""><OPTION>Inget valt</OPTION></SELECT>" & vbCrlf
??Sv: Skicka text, inte Array Index.
Är arrayerna statiska eller skapas det från en databas? Du kan ju slå upp värdena på ASP sidan:
<code>
<TD class="liten">
<div align="right">
<input type="text" name="txtomrade2" Value="<%
Select Case Request.Form("fieldname2")
Case 1
Response.write "Applikationsdrift"
Case 2
Response.write "Server"
End Select
%>" size="25" maxlength="25">
</div>
</TD>
<TD class="liten">
<div align="right">
<input type="text" name="txtomrade2" Value="<%
Select Case Request.Form("fieldname2")
Case 1001
Response.write "Applikationsdrift s/390"
Case 1002
Response.write "Applikationsdrift AS/400"
Case 2001
Response.write "Tekniskdrift S/390"
Case 2002
Response.write "Backup"
End Select
%> size="25" maxlength="25">
</div>
</TD>
</code>Sv: Skicka text, inte Array Index.
******
function clickcombo(nWhich,elem1,elem2){
if (nWhich == 1){
clearcombo(elem2);
populatecombo2(elem2, elem1[elem1.selectedIndex].value);
}
return true;
}
function getSelectedValue(intSelected)
{
document.form1.strHidden.value=document.form1.sparte[intSelected].text
}Sv: Skicka text, inte Array Index.
********************
<%
Response.Write TripleLinkedList(cnn, sQuery, "fieldname", 1, "tjansteomradenamn", "tjanstebesnamn")
%>
********************
Function TripleLinkedList(oCon, sQuery, sFormFieldName, nSize, sDBField1, sDBField2)
' the function is passed the following parameters:
' oCon object reference to an open ADODB.Connection object
' sQuery string containing the fully-articulated SQL query
' sFormFieldName string containing the name for these fields inside the HTML form definition
' note: the fields are named with suffixes 1, 2, 3
' nSize the size (number of entries) for the listboxes
' sDBField1 field name that goes into the first listbox
' sDBField2 field name that goes into the second listbox
' sDBFieldResult the name of the field returned a the result of
Dim sTemp ' general-purpose temp variable
Dim sScript ' bucket for holding the script structure
Dim sSelect ' bucket for the <SELECT> statement
Dim sArray1 ' bucket to store the DBField2 array
Dim nField1 ' counter for the primary array
Dim sArray2 ' bucket to store the DBField3 array
Dim nField2 ' counter for the secondary array
Dim rs ' recordset
Dim sLastVal1 ' comparison string to test for record changes
Dim sLastVal2 ' comparison string to test for record changes
On Error Resume Next
If Not IsObject(oCon) Then
sScript = "error processing triplelist -- need a connection object."
ElseIf oCon.State <> 1 Then
If Err.number <> 0 Then
sScript = "error processing triplelist -- invalid connection object."
Else
sScript = "error processing triplelist -- connection is not open."
End If
Else
Set rs = oCon.Execute(sQuery)
If Err.number <> 0 Then
sScript = "error processing query. Error " & Hex(Err.number) & ": " & Err.Description
ElseIf rs.EOF Then
sScript = "no records found -- seems wrong"
Else
On Error Goto 0 ' reset the error handler.
' okay, we got some records... good. Now let's process them.
' general-purpose header information goes here...
' -----------------------------------------------------------
' first write the script header into the "sScript" bucket
' then declare the arrays into the script...
' -----------------------------------------------------------
sScript = "<SCR" & "IPT LANGUAGE=""JavaScript"">" & vbCrlf
sScript = sScript & "var array1 = new Array();" & vbCrlf
sScript = sScript & "var array2 = new Array();" & vbCrlf
' -----------------------------------------------------------
' create the first of the input field list box/combo box
' and set its values
' -----------------------------------------------------------
sSelect = "<SELECT NAME=""" & sFormFieldName & "1"" SIZE=""" & nSize & _
""" ONCHANGE=""return(clickcombo(1,document.forms[0]." & _
sFormFieldName & "1,document.forms[0]." & sFormFieldName & _
"2,document.forms[0]." & sFormFieldName & "3));"">" & vbCrlf
' -----------------------------------------------------------
' okay, now let's start to loop through the query
' then declare the arrays into the script...
' -----------------------------------------------------------
sLastVal1 = "empty" ' set up a default test value...
Do Until rs.EOF
If rs(sDBField1) <> sLastVal1 Then
' if the contents of field 1 changed, then
' close any previous entries for field1, 2, 3
If Right(sArray1, 3) = "," & vbCrlf Then
' pull off any trailing commas
sArray1 = Left(sArray1, Len(sArray1) - 3)
End If
If Len(sArray1) > 0 Then
sArray1 = sArray1 & ");" & vbCrlf
End If
' pick up new information for this row...
nField1 = nField1 + 1
sLastVal1 = rs(sDBField1)
' write the new contents of field 1 to the select statement
sSelect = sSelect & "<OPTION VALUE=" & nField1 & ">" & sLastVal1 & "</OPTION>"
' write a new entry in array1 for the field 2 values...
sArray1 = sArray1 & "array1[" & nField1 & "] = new Array(" & vbCrlf
' and reset the test values for field 2
nField2 = 0
sLastVal2 = "empty"
End If
If sLastVal2 <> rs(sDBField2) Then
' if the contents of field 2 changed, then
' close the previous entry for sArray2
If Right(sArray2, 3) = "," & vbCrlf Then
' pull off any trailing commas
sArray2 = Left(sArray2, Len(sArray2) - 3)
End If
If Len(sArray2) > 0 Then
sArray2 = sArray2 & ");" & vbCrlf
End If
' pick up new information for this row...
sLastVal2 = rs(sDBField2)
nField2 = nField2 + 1
' write a new entry in array1 containing this set of field 2 values...
sArray1 = sArray1 & " " & 1000 * nField1 + nField2 & ",""" & sLastVal2 & """," & vbCrlf
' write a new entry in array2 for this set of field 3 values...
sArray2 = sArray2 & "// values for " & sLastVal2 & vbCrlf
sArray2 = sArray2 & "array2[" & 1000 * nField1 + nField2 & "] = new Array(" & vbCrlf
End If
rs.MoveNext ' move on to the next record...
Loop
' if these arrays weren't previously closed out, then close them now
If Right(sArray2, 3) = "," & vbCrlf Then
' pull off any trailing commas
sArray2 = Left(sArray2, Len(sArray2) - 3)
End If
If Len(sArray2) > 0 Then
sArray2 = sArray2 & ");" & vbCrlf
End If
' if these arrays weren't previously closed out, then close them now
If Right(sArray1, 3) = "," & vbCrlf Then
' pull off any trailing commas
sArray1 = Left(sArray1, Len(sArray1) - 3)
End If
If Len(sArray1) > 0 Then
sArray1 = sArray1 & ");" & vbCrlf
End If
' close out the listbox/combobox and add the second listbox/combobox entries...
sSelect = sSelect & "</SELECT>" & vbCrlf
sSelect = sSelect & "<SELECT NAME=""" & sFormFieldName & "2"" SIZE=""" & nSize & _
""" ONCHANGE=""return(clickcombo(2,document.forms[0]." & sFormFieldName & _
"1,document.forms[0]." & sFormFieldName & "2,document.forms[0]." & _
sFormFieldName & "3));""><OPTION>Inget valt</OPTION></SELECT>" & vbCrlf
' finally clean up the script and write the whole schmear out as a block
sScript = sSelect & vbCrlf & _
sScript & vbCrlf & _
sArray1 & vbCrlf & _
sArray2 & vbCrlf & _
"</SCR" & "IPT>" & vbCrlf
' -----------------------------------------------------------------
End If
rs.Close
Set rs = Nothing
End If
TripleLinkedList = sScript
End Function
%>