我试图创建自动填充文本框,我的jquery有问题。请更正我失踪的地方。
以下是我尝试的代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function () {
var arr_text = [{"cityid":20,"city":"city1"},{"cityid":21,"city":"city2"},{"cityid":22,"city":"city3"}]
$("#txtauto").autocomplete({
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
var matching = $.grep(arr_text, function (value) {
var name = value.city;
var id = value.cityid;
return matcher.test(name) || matcher.test(id);
});
response(matching);
}
});
});
</script>
</head>
<body>
<div class="ui-widget">
<form>
<label for="txtauto">Autocomplete</label>
<input id="txtauto" />
</form>
</div>
</body>
</html>
的输出
答案 0 :(得分:0)
嗨使用如下的响应。
response( $.map( matching, function( item ) {
return {
label: item.city,
value: item.cityid
}
}));