<script>
$(document).ready(function() {
var myArr = [];
$.ajax({
type: "GET",
url: "airports.xml",
dataType: "xml",
success: parseXml,
complete: setupAC,
failure: function(data) {
alert("XML File could not be found");
}
});
function parseXml(xml)
{
//find every query value
$(xml).find("airport").each(function()
{
myArr.push($(this).attr("label"));
});
}
function setupAC() {
$("input#depart_from").autocomplete({
source: myArr,
minLength: 1,
select: function(event, ui) {
$("input#depart_from").val(ui.item.value);
$("#submitform").submit();
}
});
}
});
</script>
这是我的输入元素
<input id="depart_from" type="text" name="depart_from" placeholder="Depart from"/>
有什么建议吗?
答案 0 :(得分:2)
好的,修改后的答案。将dataType更改为html并修复xml文档中的错误:
$(document).ready(function() {
var myArr = [];
function parseXml(xml)
{
//find every query value
$(xml).find("airport").each(function()
{
myArr.push($(this).attr("label"));
});
}
function setupAC() {
$("input#depart_from").autocomplete({
source: myArr,
minLength: 1,
select: function(event, ui) {
$("input#depart_from").val(ui.item.value);
$("#submitform").submit();
}
});
}
$.ajax({
type: "GET",
url: "airports.xml",
dataType: "html",
success: parseXml,
complete: setupAC,
failure: function(data) {
alert("XML File could not be found");
}
});
});