是否有来自xml的自动完成搜索的示例脚本,然后将值放入隐藏字段?
示例xml:
<geoname><name>London</name><geonameId>2643743</geonameId><countryCode>GB</countryCode><countryName>United Kingdom</countryName></geoname>
搜索名称时,geonameId值将被放入隐藏字段。
的javascript:
<script type='text/javascript'>
$(window).load(function(){
$.ajax({
type: "GET",
url: "Region.xml", // change to full path of file on server
dataType: "xml",
success: function(xmlResponse) {
var data = $("geoname", xmlResponse).map(function() {
return {
value: $("Name", this).text() ,
id: $("geonameId", this).text()
};
}).get();
$("#test").autocomplete({
source: function(req, response) {
var re = $.ui.autocomplete.escapeRegex(req.term);
var matcher = new RegExp("^" + re, "i");
response($.grep(data, function(item) {
return matcher.test(item.value);
}));
},
minLength: 2,
select: function(event, ui) {
$("#result").html(ui.item ? ui.item.id : "Nothing selected, input was " + this.value);
}
});
}
});
});
</script>
html
<div class="ui-widget">
<input id="test" />
</div>
<input type="hidden" name="result" value="">
如何将结果(geonameId)放入隐藏字段?