如何使用JSON数据填充文本字段

时间:2013-02-20 19:13:46

标签: jquery

我有

jQuery(document).ready(function(){
 $('#location_search').autocomplete(
    {
     source:'suggest_location.php',
     minLength:1});
    });

它返回JSON数据:

[
    {
        "label" : "EB\u0130 Konukevi",
        "value" : "EB\u0130 Konukevi, 32.791, 39.888",
        "hor" : "32.791",
        "ver" : "39.888"
    },
    {
        "label" : "EB\u0130 Gretwes",
        "value" : "EB\u0130 Gretwes, 32.491, 39.488",
        "hor" : "32.491",
        "ver" : "39.488"
    }
]

当我选择其中一个修饰时,它会填充location_search字段。

如果选择了某个位置,我将如何使用hor和ver填充隐藏文本输入(带有id的ver和hor)?

1 个答案:

答案 0 :(得分:2)

我想我明白你在找什么...尝试使用select event

$('#location_search').autocomplete({
    source:'suggest_location.php',
    minLength:1,
    select: function( event, ul ) {
       //do something with hor, ver
       $('#hor').val(ul.item.hor); 
       $('#ver').val(ul.item.ver);
    } 
 });

DEMO:http://jsfiddle.net/dirtyd77/yTMwu/50/