PDA

View Full Version : select boxes


dfred
09-25-2002, 06:42 PM
so here is my problem. first off i am using cold fusion. I have select boxes dynamically named depending on how many there should be. I also have a default value in both select boxes. if the user wants to change a value in one of the boxes(which is populated from the db), the 2nd select box will be populated with a new value(grabbed from the db based on the selection from the 1st select box). the real problem is i cannot get the .option[ind] to recognize the 'ind'. it seems since it is a dynamically named select box that the name and the " symbols are messing it up. the following is the code i am using so far:

<script>
<cfoutput>
function h_part_change(){
var p = #get_SCR_lines.scr_line_nbr#; //this is the value used as a looping value
var hgear = eval("document.forms[0].hgears1"+"_"+p+".value");
//following is the query used to create the arrays for the two select boxes
<cfquery name="hgear_part_desig_qn1" datasource="#session.dbase#" username="#session.uname#" password="#session.pword#">
select a.part_number part_nbr, a.hgear_desg hgear_desg
from tlos_specs a, stk_hgear_weapon_specs b
<cf_hgear_where>
order by part_number
</cfquery>
//two arrays for each select box
var h_pn = new Array(#quotedValueList(hgear_part_desig_qn1.part_nbr)#);
var h_desg = new Array(#quotedValueList(hgear_part_desig_qn1.hgear_desg)#);

for(i=0; i<h_desg.length; i++){
if (h_desg[i] == hgear){
//alert(h_pn[i] + ' is the part number now being filled into the Associate with Part No. field');
for (ind = 0; ind<3; ind++)
{
var option_text = "document.forms[0].assoc_part_nbr1_"+p+".options[ind].text";
//alert(option_text);
if (eval(option_text) == h_pn[i] )
{
alert(ind);
var option_sel = "document.forms[0].assoc_part_nbr1_"+p+".options[ind]";
alert(option_sel);
}
}
}
}
}
</cfoutput>
</script>


//first select box:
<cfoutput><select name="hgears1_#get_SCR_lines.scr_line_nbr#" onChange="return h_part_change();"></cfoutput>
<option value="">--</OPTION>
<cfoutput query="hgear_part_desig_qn1">
<cfset hgear = "#hgear_part_desig_qn1.hgear_desg#">
<option value="#hgear#" <cfif #hgear# IS #get_assocs1.HGEAR_DESG#>selected</cfif>>
#hgear#</OPTION>
</cfoutput>
</select>

//second select box:
<cfoutput><select name="assoc_part_nbr1_#get_SCR_lines.scr_line_nbr#"></cfoutput>
<cfoutput query="hgear_part_desig_qn1">
<cfset hgear = "#hgear_part_desig_qn1.h_part_no#">
<option value="#hgear#" <cfif #hgear# IS #get_assocs1.ASSOC_PART_NBR#>selected</cfif>>
#hgear#</OPTION>
</cfoutput>
</select>

thanks for any help
dan

dfred
09-26-2002, 01:26 PM
Please help me. I cannot seem to get js to recognize the [ind] in the var section.

dan

RadarBob
09-26-2002, 02:42 PM
Wow, dfred, I just might make this worse!

First off, I don't understand all that # stuff. I see that it has to do with referencing the results of your <cfquery>, but I've never seen that stuff before. Second, I don't know if your script is executing on the client or server. It makes a difference in how you code up a dynamic <select> list.

Nonetheless I may have a couple of useful observations.

First, if I'm building a dynamic list of <option>s inside of a <select> I expect to see some sort of looping mechanism. That's how it's done with Active Server Pages Recordsets (on the server side). Even client-side I'd expect some kind of loop if your building from an array.

Second, the javascript for creating a new <option> is to create a new Option object. This statement would be inside that afore mentioned loop.

Third, writing HTML from within javascipt is done with document.write".

Fourth, when creating new Option thingies *on the client side*, one does not actually spit out HTML. See, building/changing/etc. a <select> object is literally dynamic - you don't re-write or re-draw the whole page! Simply creating a new Option puts it on the list. Cool, eh?

So on the server it might look like this. This example is with ASP and VBScript; but you can see the fundamental code structuring:

<select name="fUpdFreq">
<%
rsUpdateFreq.MoveFirst()
While (NOT rsUpdateFreq.EOF)
%>
<OPTION value="<%=(rsUpdateFreq.Fields.Item("CodeValue").Value)%>">
<%=(rsUpdateFreq.Fields.Item("CodeText").Value)%></OPTION>
<%
rsUpdateFreq.MoveNext()
Wend



Now on the client it might look like this, in Javascript of course:

function InsertNewKeyword(theform) {
selectIndex = theform.newList.length;
x = theform.newList.options;
// so I don't have to type so much later.

x[selectIndex] = new Option (text for VALUE, text for TEXT, false, false);
// 1st false = not selected by default,
//2nd false = not selected now
} // InsertNewKeyword ()



Am I WAY off base here?