我使用 jqueryUI 自动完成来搜索地理名称,但我无法为我的文本框设置值,这是我的代码:
<script>
$(function () {
function log(message) {
$("#appendedInputButton").val(message);
}
$("#appendedInputButton").autocomplete({
source: function (request, response) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function (data) {
response($.map(data.geonames, function (item) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
},
minLength: 1,
select: function (event, ui) {
log(ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function () {
$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
},
close: function () {
$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
}
});
});
</script>
我的 HTML 是:
<div class="input-append">
<form role="search" method="get" id="searchform" action="http://sushiant.com/">
<button class="btn" type="submit" id="searchsubmit">جستجو</button>
<input class="span2" name="appendedInputButton" id="appendedInputButton" size="16" type="text" style="width: 300px;">
</form>
</div>