I am trying to select an item in a ListBox based on text entered in a textbox using jquery. If the length of the text entered in the textbox is greater than 1 I would like to loop through the items in the ListBox and compare the value of each item and if it matches the numbers entered in the textbox I need to select/highlight it in the ListBox. Here is what I am doing but doesn t seem to work. The ListBox ListItem s are populated at runtime from the database.
Asp.Net
<asp:TextBox ID="txtMediaCode" runat="server" MaxLength="2" Width="40px" />
<asp:ListBox ID="lsMediaCodes" runat="server" Width="296px" />
Jquery
<script type="text/javascript">
$(document).ready(function () {
$( #txtMediaCode ).keyup(function () {
if ($( #txtMediaCode ).length > 1) {
$( #lsMediaCodes ).each(function (i, option) {
if ($(option).val() == $( #txtMediaCode ).val()) {
$(option).attr( selected , selected );
}
});
}
});
});
</script>
是否有任何建议?