【问题标题】:How to display Json Data in JQuery autocomplete UI?如何在 JQuery 自动完成 UI 中显示 Json 数据?
【发布时间】:2013-03-08 18:48:28
【问题描述】:

我正在使用 jquery 自动完成 ui,我正在获取这样的 json 格式的数据

[{"organization_name":"health info"},{"organization_name":"Canada health"},{"organization_name": "org 1"}]

这是我的 jquery 代码,它没有正确显示 json 数据

 $('input[name=profileOrg]').autocomplete({
                    source:'CHI_custom/customScripts/getorgname.php',
                    dataType: 'json',
                    minLength:2

                });

谁能帮助如何在自动完成文本框下方显示数据?

【问题讨论】:

  • 你读过文档吗
  • 您的 JSON 中需要 label: str, value: str 对。
  • @Ohgodwhy 你的意思是这样的[{lable:organization_name,value:health info},{lable:organization_name,value:value2}]
  • 来源格式为[{value:'health info', label:'health info'}]

标签: php jquery json


【解决方案1】:

您需要更改自动完成显示项目的方式

$('input[name=profileOrg]').autocomplete({
    source:'CHI_custom/customScripts/getorgname.php',
    dataType: 'json',
    minLength:2,
    select: function (event, ui) {
        $(this).val(ui.item.organization_name);
        return false;
    }
})
.data("autocomplete")._renderItem = function (ul, item) {
        return $("<li></li>")
          .data("item.autocomplete", item)
          .append('<a>' + item.organization_name + '</a>')
          .appendTo(ul);
};

有关 jQuery 文档的更多信息:http://jqueryui.com/autocomplete/#custom-data

【讨论】:

  • 谢谢你的代码对我有用,非常感谢......你是我的救星
  • 您的代码工作正常,但是当我从列表中选择名称时,该名称不会显示在文本框中
  • 你是对的。您需要将select 选项添加到函数中。答案已编辑。
  • 非常感谢您节省了我的时间,但我需要在选择标签中显示数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多