Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery ui 关于一个字段下多个值的建议-jQueryUI自动完成_Jquery Ui_Autocomplete - Fatal编程技术网

Jquery ui 关于一个字段下多个值的建议-jQueryUI自动完成

Jquery ui 关于一个字段下多个值的建议-jQueryUI自动完成,jquery-ui,autocomplete,Jquery Ui,Autocomplete,首先,对这个问题的标题感到抱歉。我想了很长时间,没有比这更好的了 我的问题是:jQueryUI的自动完成功能能否从一个自动完成字段下的多个数据库字段提供建议。例如,我希望能够在字段中键入br,并使briman057和Brian Johnson都显示为建议,即使它们存储在单独的数据库字段中,并作为同一JSON项的两个单独的键值对返回。[{用户名:briman057,名称:'Brian Johnson']}。然后,我希望用户名值是在选择它或全名时填充该字段的值。我知道其中一个键需要命名为value或

首先,对这个问题的标题感到抱歉。我想了很长时间,没有比这更好的了


我的问题是:jQueryUI的自动完成功能能否从一个自动完成字段下的多个数据库字段提供建议。例如,我希望能够在字段中键入br,并使briman057和Brian Johnson都显示为建议,即使它们存储在单独的数据库字段中,并作为同一JSON项的两个单独的键值对返回。[{用户名:briman057,名称:'Brian Johnson']}。然后,我希望用户名值是在选择它或全名时填充该字段的值。我知道其中一个键需要命名为value或label,并且使用该名称的键是用于提供建议的键,但是我可以为一个JSON项使用两个值键吗?

可以,因为您可以提供自己的自定义回调作为数据源。该回调可以实现您想要的行为

这是我想你想要的东西的演示

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/ui-lightness/jquery-ui.css" type="text/css" media="all" />
<script type="text/javascript">
$(function() {
  $('#myautocomplete').autocomplete({
    source: function(request, response) {
      // TODO: make suggestions actually depend on what the user types into the search box.

      // You want two suggestions, one with 'briman57' and one with 'Brian Johnson', so we need two suggestions with different labels, but with the same value.
      var suggestions = [
        { label: 'briman057'    , value: 'briman057', username : 'briman057', name : 'Brian Johnson'},
        { label: 'Brian Johnson', value: 'briman057', username : 'briman057', name : 'Brian Johnson'}
      ];
      response(suggestions);
    },
    select: function( event, ui ) {
      alert('You have selected ' + ui.item.name + ' (' + ui.item.username + ')');
    }
  });
});
</script>
<input id="myautocomplete" title="Enter anything here."></input>

请查看的源代码以获取更多灵感。

感谢添加编辑。我看了一下链接,还是有点不清楚,但我现在明白了。