I survived this pain using MVC, and the returned object should have a label property (displayed in the drop-down list) and value property (the actual choice value is UnivCode).
The key to it is the selection method defined in your autocomplete field that takes 2 arguments (for example, select: function (e, ui) {... do stuff ...; return false;}). The trick here is to return false to prevent the default event handler from running jQuery.
Then you can use ui.item.label to get the displayed value and ui.item.value to get the code.
I used a separate method that took ui.item and then wrote the values โโfor hidden inputs.
If I can get the code together, I'll post an example.
Example: - This example uses the Autocomp1_display text field, to which autocomplete is attached. The displayItem method then writes the displayed value of the selected item to this text box and puts the selected value in a hidden range.
$j("#Autocomp1_display").autocomplete({ source: function(request, response){ $j.ajaxSetup({cache: false}); $j.ajax({ url: "AutoComplete.asmx/GetUniversities" type: "GET", data: request, dataType: "json", success: function (data) { request.term=""; response(data); } }); }, cache: false, select: function(e, ui){ displayItem("Autocomp1", ui.item); Autocomp1_HasSelections = true; setAutocompleteState("Autocomp1_display", Autocomp1_IsMultiSelect, Autocomp1_HasSelections); $j('#Autocomp1').change(); return false; }, focus: function(event, ui){ if (ui.item.label){ $j(this).val(ui.item.label); } else { $j(this).val(ui.item.value); } return false; } });
The select event uses the displayItem method as shown below
//Function to write the entries into the text box function displayItem(target, dataObject) { var displayValue = dataObject.label.replace('\n',''); var existingSpan = false; existingSpan = document.getElementById("span_" + displayValue); if (!existingSpan) { var span = $j("<span>").text(displayValue); //Create a new span tag to display the selected value span.attr({ id: "span_" + dataObject.value }); var hiddenFld = $j("<input>").attr({ type: "hidden" }).val(dataObject.value); //Create a div object to store the code value hiddenFld.addClass(target + "IdField").css("visibility", "hidden").css("height", 0).attr({ id: target, name: target }); //Format the div var a = $j("<a>").addClass(target + "remove").attr({ href: "javascript:", title: "Remove " + displayValue }).text("x").appendTo(span); //Define the "x" to remove the span hiddenFld.appendTo(span); //Append the div to the span span.insertBefore("#" + target + "_display"); //Insert span before the concealed text box $j("#" + target).attr({ value: dataObject.value }); //Store the ID value related to the selected item $j("#" + target + "_display").val("").css("top", 2); //Store the ID value related to the selected item //$j("#" + target + "_display").flushCache(); //Flush the cache for the autocomplete control } else { alert("This item has already been selected"); } }
SetAutocompleteState method: -
function setAutocompleteState(targetName, IsMultiSelect, HasSelections) { if (!IsMultiSelect && HasSelections) { $j("#" + targetName).autocomplete("option", "disabled", true); } else { $j("#" + targetName).autocomplete("option", "disabled", false); } }
The definitions of isMultiSelect and HasSelections determine whether to enable autocomplete or not, and targetName is just the identifier of the text field that was "autocomplete"