开发者

post all values in select box /listbox

I have a selectbox on a form - which I've turned in to a list box by putting

 <select id="Select1" name="D1" size="5" style="width: 220px">

I'm filling this select/listbox with values...

When I post the form how can I get all the values in the select box..is this possible or am I only able to get one that has been selected.

T开发者_开发百科rouble is I want all the values in the select (I'm not selecting any as such)

Any ideas?


Before submitting the form you can use some JavaScript to pull the items out of the select and put them into a hidden text field (as a delimited string)

For example, you can get the values using

var select1 = document.getElementById('select1');
var values = new Array();

for(var i=0; i < select1.options.length; i++){
    values.push(select1.options[i].value);
}

var allValues = values.join(";");
alert(allValues);

Hope that helps.


How are you adding the values to the list box? Are they static or are they pulled from a database.

If you're pulling from the database I would create a function that you use to get the data and bind to the list box.

Then use that same function when you want to get those values after the post. You may have to use some hidden fields to pass along any parameters you use to get the values for the list box in the first place.

example:

function get_models_for_make(int make_id)
  mydata_rs = SELECT name, id FROM models WHERE make_id = make_id
  return mydata_rs
end

so you could use this data to bind the objects to your listbox and also use it to get the values later that you did bind to your list box.


for (int i = source.Items.Count - 1; i >= 0; i--)
        {
            ListItem item = source.Items[i];

            if (moveAllItems)
                item.Selected = true;

            if (item.Selected)
            {
                // if the target already contains items, loop through
                // them to place this new item in correct sorted order
                if (target.Items.Count > 0)
                {
                    for (int j = 0; j < target.Items.Count; j++)
                    {
                        if (target.Items[j].Text.CompareTo(item.Text) > 0)
                        {
                            target.Items.Insert(j, item);
                            item.Selected = false;
                            break;
                        }
                    }
                }

                // if item is still selected, it must be appended
                if (item.Selected)
                {
                    target.Items.Add(item);
                    item.Selected = false;
                }

                // remove the item from the source list
                source.Items.Remove(item);
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜