On 26/03/07, Vincent <vince@storesprite.org.uk>
wrote:>
> Hi All,
>
> After much searching and head scratching I post you this!
>
> I can't seem to find a generic way to handle multiple select data. The
> function I use to receive form values will only pass the first selected
> value from a multiple select. I have pasted part of the form and the
> function below.
>
> Thanks for any pointers,
>
> Vince
>
>
> // Example selectbox
>
> <select name="countryarray[]"
multiple="multiple">
> <option value="AF">Afghanistan</option>
> <option value="AL">Albania</option>
> <option value="GY">Alderney</option>
> <option value="AS">American Samoa</option>
> <option value="AD">Andorra</option>
> <option value="AG">Antigua and Barbuda</option>
> <option value="AM">Armenia</option>
> <option value="AW">Aruba</option>
> </select>
>
>
> // Form Function
>
> function getformvalues (fobj, valfunc){
> var str = "";
> aok = true;
> var val;
> //Run through a list of all objects within the form.
> for(var i = 0; i < fobj.elements.length; i++){
> if(valfunc) {
> if (aok == true){
> val = valfunc
(fobj.elements[i].value,fobj.elements[i].name);
> if (val == false){
> aok = false;
> }
> }
> }
>
> if (fobj.elements[i].type == "checkbox")
{
> if (fobj.elements[i].checked) {
> str += fobj.elements[i].name +
"=" +
> escape(fobj.elements[i].value) + "&";
> } else {
> str += fobj.elements[i].name +
"=&";
> }
>
> } else if (fobj.elements[i].tagName ==
"SELECT") {
> var sel = fobj.elements[i];
> str += sel.name + "=" +
> sel.options[sel.selectedIndex].value + "&";
>
> } else {
> str += fobj.elements[i].name +
"=" + escape(fobj.elements[i].value)
> + "&";
> }
> }
> //Then return the string values.
> return str;
> }
>
>
> >
>
From the MS HTML Reference ...
SELECT
HTML N/A
Scripting select.selectedIndex [ = iIndex ]
The selectedIndex property is most useful when used with SELECT
objects that support selecting only one item at a time�that is, those
in which the MULTIPLE attribute is not specified. If the MULTIPLE
attribute is specified for a SELECT object, the selectedIndex property
returns only the index of the first selected item, if any.
and
OPTION
HTML <OPTION SELECTED ... >
Scripting select.options[iIndex].selected [ =bSelected ]
The property determines whether a value is submitted with the form. If
the value of the control matches the default value, the control's
value is not submitted. The value is submitted only when the control's
value does not match the default value.
To select an item in HTML, it is not necessary to set the value of the
SELECTED attribute to true. The mere presence of the SELECTED
attribute set its value to true.
So, if select.multiple is true, then you need to iterate the OPTIONS
collection looking for option[i_opt].selected = true.
--
-----
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
"Standing on the shoulders of some very clever giants!"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to rubyonrails-spinoffs@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---