当有人在输入字段中键入邮政编码但我无法填写下拉列表时,我正在尝试自动完成下拉列表,但这就是我所在的位置:
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>
并采用以下形式:
<input id="postcodeSearch" name="postcodeSearch" type="text" />
<select id="ajaxPostCodeList">
</select>
脚本中的以下部分不起作用:
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);
}
},
控制器
[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());
}
因此,搜索文本可以正常传递到控制器中,但我不知道该怎么做是用值填充下拉列表。
非常感谢任何帮助!