I have fields that a user would enter and i would pass it into jQuery and pass to a web service.
I can get textboxes values by:
var name = $(“#MainContent_txtName”).val()
The problem I’m having is a multi-select that comprises of a list item collection.
If I was to do this server side, I would do something like:
foreach (ListItem li in listTitles)
{
if (li.Selected)
{
selectedValue += li.Value + ",";
}
}
And this would give me the string of selected values from the select list.
I’m having trouble getting it out from jQuery.
If I do
var titles = $("#MainContent_selectListTitles").val()
That is obviously incorrect because it won’t bring back the selected list items.
I saw a post that suggested that I could retrieve it if I say option selected.
So I tried this:
var titles= $('#MainContent_selectListTitles option:selected');
The next thing I did was pop an alert to see what the titles were. It just said [object, object].
So my questions are:
-
Is it possible to get the selected items from the list item collection concatenated into a string?
-
Or is it better that I get all the form values from a postback even on my code behind and then call the jquery function? If this is an option, i’ve attempted to do this but have failed. It tells me that it can’t find the method. So i’m definitely not calling the jquery function correctly on postback of the button event.
THanks.
First, make sure your item id is correct on client side that is “MainContent_selectListTitles”. You can check it by “View Source”.
If the ID is correct, follow this.
Every time there is changes, it will call this function which means it will make your web service busy. You can move the above code to button click event if you want.