Originally posted by Keith Harrison: ...How would the page refresh once you have chosen a value? ...
Well, it would sure be nice if you didn't need to "refresh" the page (get the server to provide a new version) every time the user picks an option, right?
This sounds like something that would be better if programmed on the client (browser) side, so that the options can change without going back and forth with the server. For that, something like JavaScript would be good.
I'm going to go out on a limb, and move this to the JavaScript forum.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
6
posted
0
I am coding this here in this textarea so hopefully there will be no bugs!
Eric [ August 17, 2006: Message edited by: Eric Pascarello ]
Keith Harrison
Greenhorn
Joined: Mar 04, 2006
Posts: 15
posted
0
Thats very helpful. Thanks very much Eric.
Keith Harrison
Greenhorn
Joined: Mar 04, 2006
Posts: 15
posted
0
Referring to the previous post by Eric, does anybody know a way that I can set the selected option if I know its value?
For example, I have a subCategory value that�s being passed back from a Servlet as a request attribute and would like to set the relevant option as selected.
The array is declared as follows: selectOptions['SHEETS'] = new Array('Choose a sub category','Flat', 'Centre Folded', 'Multi Folded');
On loading the page, I want to assign the option 'Flat' as being selected within the following code:
//Create a new option (text,value) var newOpt = new Option(selectOptions[selectedItem][i], selectOptions[selectedItem][i]); //append it to the options list sel.options[sel.options.length] = newOpt;
Any help or guidance will be appreciated.
Thanks very much.
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15357
6
posted
0
You can, have to call the function onload and have the servlet set a variable or hidden field with the value that needs to be selected.
change AddOPtions to accept new parameter
change code to see if we should select it
Eric [ October 30, 2006: Message edited by: Eric Pascarello ]
Keith Harrison
Greenhorn
Joined: Mar 04, 2006
Posts: 15
posted
0
Thanks Eric.
I can see the logic behind your reply but I cant seem to piece it together. At the beginning of my page I obtain the request attributes:
Then further down my page I have the select objects:
Is there an easier way to set the select1 option instead of doing this:
Once I have set the select1 (category) option, I need to call the onchange AddOption function to fill select2 (subCategory). I then need to call the select2 toggleOther function which already enables or disables other input boxes.
Setting select1 using your solution works fine but I want select2 to have a selected option. Eric showed me a way of doing this further up the same post but for some reason its not getting recognised.
<script type="text/javascript">
var selectedValue = "<%=subCategory%>";
window.onloaad = function(){ var sel = document.frmSearch.select1; var val = sel.options[sel.selectedIndex].value; AddOptions(val,selectedValue); } </script>
What I am finding is when the page loads, the two select objects get populated but select2 always has 'Choose a sub category' as the selected option when 'Flat' is being returned from the servlet.
Any ideas?
Thanks very much. [ November 01, 2006: Message edited by: Keith Harrison ]
Keith Harrison
Greenhorn
Joined: Mar 04, 2006
Posts: 15
posted
0
Sorry to bring this one up again Eric but I have tried meddling with this problem numerous times now and cannot find a solution.
The answer may be simple but I cannot fathom it out!
When my page reloads with records returned by a search, Select2 does not have the correct option selected, even though your example does correctly evaluate the right option to be selected. Three options are being created and even though the second option should be selected, the first always gets selected.