我正在尝试创建一个脚本,该脚本将使用 JQuery 的自动完成文本框来显示已按州过滤的数组中的县数据。状态是一个下拉列表。例如:如果用户选择了“伊利诺伊州”作为州,根据他们在文本框中输入的乞求字母,自动补全会为他们提供最接近的县名。我成功地能够按状态过滤数组,然后用正确的数据加载数组,但我在尝试将数组加载到自动完成时遇到问题。这是代码段。非常感谢您的帮助:
<body >
<script type="text/javascript">
function findCounties(State)
{
var County = new Object();
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp != null)
{
xmlhttp.open("GET","US_Counties.csv",false); // the false makes this synchronous!
xmlhttp.send();
var text = xmlhttp.responseText;
// text contains the ENTIRE CONTENTS of the text file
// you *could* just write those contents directly to the HTML output:
// but you might want to process that one line at a time. if so:
var lines = text.split("\n");
var cntCounty = 0;
for (var n=0; n<lines.length; n++)
{
if(lines[n].indexOf(State) > 0){
var line = lines[n];
var populate = line.split(',');
County[cntCounty] = populate[1];
cntCounty++;
}//IF
}//for
$(function() {
var Counties = [{
a_state: []
};
for(var i in County) {
var item = County[i];
Counties.a_state.push({
"label" : item.County
"value" : item.County`enter code here`
});
];
j$("#counties").autocomplete({
source: Counties
}).data("autocomplete")._renderItem = function(ul, item) {
return $("<li>").data("item.autocomplete", item).append("<a>" + item.label ++ "</a>").appendTo (ul);
});