我已将此返回 json ,如何判断 来源 以获取 仅限名称 ?
var availableTags='
[
{ name: "one", num: '1', index: '0' },
{ name: "two", num: '2', index: '1' },
{ name: "three", num: '3', index: '2' },
{ name: "four", num: '4', index: '3' },
{ name: "five", num: '5', index: '4' },
{ name: "six", num: '6', index: '5' },
{ name: "seven", num: '7', index: '6' }
]';
$("#searchInput&#34)。自动完成({
来源:availableTags
});
谢谢。
答案 0 :(得分:1)
你可以试试这个:
var availableTags='
[
{ name: "one", num: '1', index: '0' },
{ name: "two", num: '2', index: '1' },
{ name: "three", num: '3', index: '2' },
{ name: "four", num: '4', index: '3' },
{ name: "five", num: '5', index: '4' },
{ name: "six", num: '6', index: '5' },
{ name: "seven", num: '7', index: '6' }
]';
var arr = JSON.parse(availableTags).map(function(a){return a.name;});
$("#searchInput").autocomplete({
source: arr
});
这个JSON没有很好的格式,这将作为js对象:
var availableTags=
[
{ name: "one", num: '1', index: '0' },
{ name: "two", num: '2', index: '1' },
{ name: "three", num: '3', index: '2' },
{ name: "four", num: '4', index: '3' },
{ name: "five", num: '5', index: '4' },
{ name: "six", num: '6', index: '5' },
{ name: "seven", num: '7', index: '6' }
];
var arr = availableTags.map(function(a){return a.name;});
$("#searchInput").autocomplete({
source: arr
});
其他选项是良好的JSON格式:
var availableTags='[{"name":"one","num":"1","index":"0"},'+
'{"name":"two", "num": "2", "index": "1" },'+
'{ "name": "three", "num": "3", "index": "2" },'+
'{ "name": "four", "num": "4", "index": "3" },'+
'{ "name": "five", "num": "5", "index": "4" },'+
'{ "name": "six", "num": "6", "index": "5" },'+
'{ "name": "seven", "num": "7", "index": "6" }]';
var arr = JSON.parse(availableTags).map(function(a){return a.name;});
$("#searchInput").autocomplete({
source: arr
});