Javascript jQuery autocomplete不会在XML数据中循环
目前,我似乎无法从XML文件中检索任何数据。下面是代码Javascript jQuery autocomplete不会在XML数据中循环,javascript,jquery,xml,jquery-ui,autocomplete,Javascript,Jquery,Xml,Jquery Ui,Autocomplete,目前,我似乎无法从XML文件中检索任何数据。下面是代码 $(document).ready(function() { setTimeout(function() { $('#_Q0').autocomplete({ source: function(request, response) { $.ajax({ url: "brands.xml",
$(document).ready(function() {
setTimeout(function() {
$('#_Q0').autocomplete({
source: function(request, response) {
$.ajax({
url: "brands.xml",
dataType: "xml",
type: "GET",
success: function(xml) {
var results = [];
$(xml).find("brand").each(function() {
if (results.indexOf(request.term.toUpperCase()) >= 0) {
results.push(results);
}
});
response(results);
}
});
},
autoFocus: true,
minLength: 3,
response: function(event, ui) {
if (!ui.content.length) {
var noResult = {
value: "",
label: "No results found"
};
ui.content.push(noResult);
}
}
});
var render = $('#_Q0').autocomplete('instance')._renderMenu;
$('#_Q0').autocomplete('instance')._renderMenu = function(ul, items) {
items.push({
label: 'AUTRE MARQUE',
value: 'AUTRE MARQUE',
last: true
});
render.call(this, ul, items);
};
}, 100);
});
这是一个砰的一声
我以前使用JSON源代码,但现在必须切换到XML
提前感谢问题在于处理xml
$(document).ready(function() {
setTimeout(function() {
$('#_Q0').autocomplete({
source: function(request, response) {
$.ajax({
url: "brands.xml",
dataType: "xml",
type: "GET",
success: function(xml) {
var term = request.term.toLowerCase();
var results = $(xml).find("brand").filter(function() {
return $(this).attr('label').toLowerCase().indexOf(term) > -1;
}).map(function() {
return {
label: $(this).attr('label')
}
}).get();
response(results, xml);
}
});
},
autoFocus: true,
minLength: 3,
response: function(event, ui) {
if (!ui.content.length) {
var noResult = {
value: "",
label: "No results found"
};
ui.content.push(noResult);
}
}
});
var render = $('#_Q0').autocomplete('instance')._renderMenu;
$('#_Q0').autocomplete('instance')._renderMenu = function(ul, items) {
items.push({
label: 'AUTRE MARQUE',
value: 'AUTRE MARQUE',
last: true
});
render.call(this, ul, items);
};
}, 100);
});
演示:您是否尝试过使用库函数?完美的非常感谢:)