I'm trying to autocomplete a drop down list when someone types a postcode into a input field but I haven t been able to fill the dropdown list yet this is where I am:
JQuery
<script type="text/javascript" language="javascript">
//<![CDATA[
// have we had at least 3 characters typed in?
var searchTextTrigger = false;
$(document).ready(function () {
$('#postcodeSearch').focus();
});
// 0m0_uk
$(function () {
var content = $('#postcodeSearch').val();
$('#postcodeSearch').keyup(function () {
// have we had at least 3 characters in our text box
if ($('#postcodeSearch').val().length >= 3) {
// yes, so after this point even if we have less it will still send the request
searchTextTrigger = true;
}
if ($('#postcodeSearch').val() != content && searchTextTrigger == true) {
content = $('#postcodeSearch').val();
var searchText = $('#postcodeSearch').val();
alert(searchText);
$.ajax({
url: "/Stores/AutocompleteSuggestions/" + etaleEncode(searchText),
success: function (data) {
$("#ajaxPostCodeList").html("");
for (var i = 0; i < data.length; i++) {
var item = data[i];
$('#ajaxPostCodeList').append($("<option></option>").val(item.Code).html);
}
},
error: function () {
alert("an error occured");
}
});
}
});
});
//]]>
</script>
and in the form:
<input id="postcodeSearch" name="postcodeSearch" type="text" />
<select id="ajaxPostCodeList">
</select>
The following section in the script is not working:
success: function (data) {
$("#ajaxPostCodeList").html("");
for (var i = 0; i < data.length; i++) {
var item = data[i];
$('#ajaxPostCodeList').append($("<option></option>").val(item.Code).html);
}
},
Controller
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult AutocompleteSuggestions(string searchText)
{
var sTerm = searchText;
if (!String.IsNullOrEmpty(searchText))
{
sTerm = Decode(searchText);
}
var suggestions = _postcodeRepository.GetAutoCompleteSearchSuggestion(sTerm);
return Json(suggestions.ToList());
}
So the search text is passing ok into the controller but what i'm not sure how to do is fill the drop down list with the values.
Any help much appreciated!