如何获取所选项目ID,名称或其他json数据?

时间:2017-04-29 18:15:38

标签: javascript jquery html json

我是JSON的新手,我还在努力了解这就是为什么我有几个问题/问题。

编辑:我实现了重定向所选项目..

我有简单的基本自动填充插件flexdatalist autocomplete,我希望获得所选的商品属性。<​​/ p>

Before you check the codes you can see my project online

脚注:我无法在codepen或stackoverflow片段上添加成就,因为在此平台上没有加载json数据。

我的json文件

[
    {
        "id": "1",
        "name": "Name 1",
        "icon" : "https://cdn3.iconfinder.com/data/icons/finalflags/16/Germany-Flag.png",
        "address":"http://www.google.com",
        "category": "Premium",
        "area": "United States",
        "updated": null
    },
    {
        "id": "2",
        "name": "Name 2",
        "icon":"https://cdn3.iconfinder.com/data/icons/finalflags/16/China-Flag.png",
        "address":"http://www.youtube.com",
        "category": "Free",
        "area": "Spain",
        "updated": null
    }
]

如您所见,我有addressicon属性,我想要返回此值,我的意思是当我从输入中选择项目时,它必须给我选择address和{{1 }}

我的js

icon

和我的HTML

    $('.flexdatalist').flexdatalist({
    cache:false,
    searchContain: false,
    textProperty: '{name},{continent}',
    valueProperty: 'address',
    minLength: 1,
    focusFirstResult: true,
    selectionRequired: true,
    groupBy: 'continent',
    visibleProperties: ["name", "continent", "capital_timezone"],
    searchIn: ["address","name", "continent"],
    url: 'hotels.json',
    relatives: '#relative'
}).on("select:flexdatalist",function(){
  window.location.href=this.value;
});

最后一个问题是如何在项目左/右添加图标/图像?图标在我的json数据中

1 个答案:

答案 0 :(得分:1)

首先 您必须更改字段以匹配您想要的字段 如果你想将json字符串更改为图像,则必须使用js

请查看此javascript是否适合您。

$('.flexdatalist').flexdatalist({
    cache:false,
    searchContain: false,
    textProperty: '{address},{icon}',
    valueProperty: 'iso2',
    minLength: 1,
    focusFirstResult: true,
    selectionRequired: true,
    groupBy: 'address',
    visibleProperties: ["address", "icon"],
    searchIn: ["address", "icon"],
    url: 'hotels.json',
    relatives: '#relative'
}).on("show:flexdatalist.results",function(ev,result){
    $.each(result,function(key,value){
        result[key]['icon_highlight'] = '<img src="'+value.icon+'">'; 
    })
});