How to get the selected value from ajax.net ComboBox through javascript
I need to get the selected value from an ajax.net combobox throught javascript so that I can do some client side validation.
What's the best way to do this? Thanks,
I've been able to get the value with this:
var combo = $get('ddlComarcas');
var comboHidd开发者_如何学编程en = $get('ddlComarcas_HiddenField');
var o4 = combo.getElementsByTagName('li')[comboHidden.value].childNodes[0];
alert('"' + o4.data + '"');
But I still need to trim the value from o4.data. Anyone can point how to do that visual studio 2008 jquery?
You can use jQuery or just use DOM:
jQuery:
var selection = $('#selectID').val();
DOM:
var selection = document.getElementById("selectID").value;
asp.net -> server side
javascript -> client side
I think the answer is value doesnt exist client side so it cant be retrieved. There are easier ways to get index tho (assuming whatever initialization is complete).
selected index: $find("<%=cboName.ClientID%>").get_hiddenFieldControl().value;
selected index (again): $find("<%=cboName.ClientID%>").get_selectedIndex();
selected text: $find("<%=cboName.ClientID%>").get_textBoxControl().value;
As far as I can tell, validating a combobox on the client requires some faith in index or text, or some kind of server side workaround.
To provide a direct answer to the subject line, a javascript array could be created server side with each combobox value and then referenced client side by selected index...
codebehind:
// write combobox values to asp:literal
foreach (ListItem i in cboName.Items)
litCboValues.Text += "\"" + i.Value.Replace("\"", "\\\"") + "\", ";
litCboValues.Text = litCboValues.Text.TrimEnd(new char[] {',', ' '});
aspx:
<script>
// array of values
var cboValues = [ <asp:Literal id="litCboValues" runat="server" /> ];
// add an alert to the combobox to test
function pageLoad()
{
$find("<%=cboName.ClientID%>").get_textBoxControl().onblur = function () {
alert( cboValues[$find("<%=cboName.ClientID%>").get_selectedIndex()] );
};
}
</script>
<asp:ComboBox id="cboName" runat="server" ...
this works (today) in IE and Chrome - about the only thing ie is good for is the debugger f12 (- you can browse through the watched objects
Following // i do it on button but you could probably do it on a combo event Follow function addFollowed() {
var combo = $get('<%= FollowListBox.ClientID %>');
var toFollow = combo.control._textBoxControl.value;
精彩评论