我需要创建一个自动完成jQuery
脚本,该脚本将过滤来自.csv(或xml等)文件的信息,选择后结果将显示所有信息。
从我的理解,我将不得不使用jquery自动完成脚本,我一整天都在学习,我希望有一个解决方案。
我有基本的+编码知识,我需要这个来过滤文本文件的结果
文本文件.csv的示例是:
"UK", "info1", "info2", "info2", "info3"
"ITALY", "info1", "info2", "info2", "info3"
"etc", "info1", "info2", "info2", "info3"
因此,当用户键入UK时,也会产生标志,点击UK后将显示info1,2,3。
我知道我需要处理这样的事情,但是如何用我自己的xml,csv等文件替换geo xml?
http://jqueryui.com/autocomplete/#remote-jsonp
还有如何加载标志?
在此示例代码中,它将从具有2个值的txt文件中进行过滤。
"12": "Italy",
我怎样才能添加更多?
示例:"12": "125": "54": "Italy",
无效
如何添加标志图像以加载?
这是完整的js:
$(function () {
'use strict';
// Load countries then initialize plugin:
$.ajax({
url: 'content/countries.txt',
dataType: 'json'
}).done(function (source) {
var countriesArray = $.map(source, function (value, key) { return { value: value, data: key }; }),
countries = $.map(source, function (value) { return value; });
// Setup jQuery ajax mock:
$.mockjax({
url: '*',
responseTime: 200,
response: function (settings) {
var query = settings.data.query,
queryLowerCase = query.toLowerCase(),
suggestions = $.grep(countries, function(country) {
return country.toLowerCase().indexOf(queryLowerCase) !== -1;
}),
response = {
query: query,
suggestions: suggestions
};
this.responseText = JSON.stringify(response);
}
});
// Initialize ajax autocomplete:
$('#autocomplete-ajax').autocomplete({
serviceUrl: '/autosuggest/service/url',
onSelect: function(suggestion) {
$('#selction-ajax').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
}
});
// Initialize autocomplete with local lookup:
$('#autocomplete').autocomplete({
lookup: countriesArray,
onSelect: function (suggestion) {
$('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
}
});
// Initialize autocomplete with custom appendTo:
$('#autocomplete-custom-append').autocomplete({
lookup: countriesArray,
appendTo: '#suggestions-container'
});
});
});
这是html:
<h2>Ajax Lookup</h2>
<p>Type country name in english:</p>
<div>
<input type="text" name="country" id="autocomplete-ajax"/>
</div>
<div id="selction-ajax"></div>
<h2>Local Lookup</h2>
<p>Type country name in english:</p>
<div>
<input type="text" name="country" id="autocomplete"/>
</div>
<div id="selection"></div>
<h2>Custom Lookup Container</h2>
<p>Type country name in english:</p>
<div>
<input type="text" name="country" id="autocomplete-custom-append" style="float: left;"/>
<div id="suggestions-container" style="position: relative; float: left; width: 400px; margin: 10px;"></div>
</div>
</div>
<script type="text/javascript" src="scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="scripts/jquery.mockjax.js"></script>
<script type="text/javascript" src="src/jquery.autocomplete.js"></script>
<script type="text/javascript" src="scripts/demo.js"></script>