duanre4421 2013-04-03 05:25
浏览 59
已采纳

Php Ajax Json搜索并填充输入字段

I need to perform a search and save it in input field dynamically. I am new to Ajax.

This is what I have done. Did a query from database and encoded using json as below

[{"id":"1","name":"Jack"},{"id":"2","name":"Ace"},{"id":"3","name":"Test"}]

Now there is a form with an input field for name (note: only existing names from database allowed). When name is being entered, I need to perform a search within Json and get name and display below the input field (exactly how google search shows related search terms) and this name should be clickable once clicked it should populate the input text with that name. And also change value of an hidden input field with respective id.

<input id="name" type="text" name="name">
<input id="id" type="hidden" name="id">

As I said before only names within the list is allowed, if a non existing name entered hidden element shouldnt have a value or 0 in it.

How can I get it done? And it should be optimised (fast) as it will have to search among a huge data say about 15k-20k names.

  • 写回答

1条回答 默认 最新

  • douchao1957 2013-04-03 07:01
    关注

    check this fiddle i created for your requirement, i used auto complete method

    http://jsfiddle.net/X85LT/

    var src = [{
    "label": "Jack",
        "value": "1"
    }, {
        "label": "back",
            "value": "2"
    }, {
        "label": "tera",
            "value": "3"
    }, {
        "label": "judo",
            "value": "4"
    }];
    
    $('.name').change(function() {
     $('.value').val(0);
    });
    
    $(".name").autocomplete({
        source: src,
        select: function (event, ui) {
            event.preventDefault();
            this.value = ui.item.label;
            $('.value').val(ui.item.value);
        }
    });
    

    // html

    <input type="text" class="name" />
    <input type="text" class="value" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。