【问题标题】:autocomplete display relevant data in autocomplete window自动完成在自动完成窗口中显示相关数据
【发布时间】:2018-11-25 14:54:06
【问题描述】:

我有 3 个输入字段,1 个用于数据类型,其他 2 个与其相关。 当我在数据类型字段中按下按钮时,我想显示像这样的自动完成窗口

而不是这个

选择后应该是这个样子

HTML

<tr>
   <td><input type="text" id="no_1" class="form-control"></td>            
   <td><input type="text" data-type="vehicle" id="vehicle_1" class="type form-control"></td>
   <td><input type="text" id="type_1" class="form-control"></td>
</tr>

JS

$(document).on('focus','.type',function(){
type = $(this).data('type');
if(type =='vehicle' )autoTypeNo = 1;   
$(this).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url : 'autocomplete.php',
            dataType: "json",
            method: 'post',
            data: {
               name_startsWith: request.term,
               type: type
            },
             success: function( data ) {
                 response( $.map( data, function( item ) {
                    var code = item.split("|");
                    return {
                        label: code[autoTypeNo],
                        value: code[autoTypeNo],
                        data : item
                    }
                }));
            }
        });
    },
    autoFocus: true,           
    minLength: 0,
    select: function( event, ui ) {
        var names = ui.item.data.split("|");                       
        id_arr = $(this).attr('id');
        id = id_arr.split("_");
        $('#no_'+id[1]).val(names[0]);
        $('#vehicle_'+id[1]).val(names[1]);
        $('#type_'+id[1]).val(names[2]);
    }              
 });
});

【问题讨论】:

  • 你能提供来自 ajax 的实际响应吗?不提供所有资源很难工作
  • 我已经在上面的图片中提供了实际的响应
  • 我的脚本可以正常工作并给出如上图 2 所示的结果,我需要对其进行一些更改才能获得如上图 1 所示的结果。
  • 好的,但是你的结果是什么样的?可以发一下吗?
  • 像图 3,但它在自动完成窗口中显示数据,像图 2

标签: javascript jquery autocomplete jquery-ui-autocomplete autocompletetextview


【解决方案1】:

在成功函数中,您需要按照以下示例形成“GL446 - 卡车 - 4 轮”之类的标签,并将值设置为“卡车”-

http://jsfiddle.net/43aeh78L/2/

你的成功方法大概是这样的-

success: function( data ) {
     response( $.map( data, function( item ) {
         var code = item.split("|");
             return {
                 label: code[0] + "-" + code[1] + "-" + code[2],
                 value: code[autoTypeNo],
                 data : item
             }
         }));
     }

编辑: 我猜item 响应函数内的对象包含所有 3 个值,然后您可以将它们附加到标签字符串为 - code[0] + "-" + code[1] + "-" + code[2]

【讨论】:

  • 如何根据我的脚本在标签中加入这3个输入字段?
  • 你的'item'对象在响应函数中是什么样子的?
  • 我已经改变了这个,但结果和图 2 一样。
  • 对象如上面的脚本号,车辆,类型。
【解决方案2】:

我在演示方面遇到了问题。 输出看起来像这样: 是你想要的吗?

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <style>

  </style>
<body>

<h1>The onclick Event</h1>

<p>The onclick event is used to trigger a function when an element is clicked on.</p>
<table>
<tr>
   <td>no:</td>            
   <td><label for="search">vehicle: </label></td>
   <td>type:</td>
</tr>
<tr>
   <td><input type="text" id="no_1" class="form-control"></td>            
   <td><input type="text" data-type="vehicle" id="search" class="type form-control"></td>
   <td><input type="text" id="type_1" class="form-control"></td>
</tr>
</table>




<script>
$(document).on('focus','.type',function(){
	type = $(this).data('type');
	if(type =='vehicle' )autoTypeNo = 1;   
	//$(this).autocomplete({
	$.widget( "custom.catcomplete", $.ui.autocomplete, {
			  _renderMenu: function( ul, items ) {
				var that = this;
				$.each( items, function( index, item ) {
					//here you modify the presentation of the label
					item.label=item.no + " - " + item.label + " - " + item.category;
					that._renderItemData( ul, item );
				});
			  }
			});

	 var data = [
		  { no:"GL-446", label: "truck", category: "4 wheel" },
		  { no:"GL-446",label: "andreastr", category: "4 wheel" },
		  { no:"GL-446",label: "antaltr", category: "4 wheel" },
		  { no:"GL-446",label: "annhhx10tr", category: "Products" },
		  { no:"GL-446",label: "annk K12tr", category: "Products" },
		  { no:"2A corsica",label: "annttop C13tr", category: "Products" },
		  { no:"GL-446",label: "anders anderssontr", category: "People" },
		  { no:"GL-446",label: "andreas anderssontr", category: "People" },
		  { no:"GL-446",label: "andreas johnsontr", category: "People" }
		];
		
	$( "#search" ).catcomplete({
		  delay: 0,
		  source: /*function( request, response ) {
				$.ajax({
					url : 'autocomplete.php',
					dataType: "json",
					method: 'post',
					data: {
					   name_startsWith: request.term,
					   type: type
					},
					 success: function( data ) {
						 response( $.map( data, function( item ) {
							var code = item.split("|");
							return {
								label: trunk,
								value: 10,
								data : 2
							}
						}));
					}
				});
			},*/
			data,
			//autoFocus: true,           
			minLength: 0,
			select: function(event, ui) {
				/*var names = ui.item.data.split("|");                       
				id_arr = $(this).attr('id');
				id = id_arr.split("_");
				$('#no_'+id[1]).val(names[0]);
				$('#vehicle_'+id[1]).val(names[1]);
				$('#type_'+id[1]).val(names[2]);*/
				if(ui.item) { 
					$( "#no_1" ).attr('value',ui.item.no);
					$( "#type_1" ).attr('value',ui.item.category);
				}
			}
    });

});



</script>

</body>
</html>

【讨论】:

  • 选择后应该像图片3
  • 对不起,我修改为在选择处添加了编号和类型。
  • 这对我的脚本来说是不可能的. 我想要图像 1 条件而不是图像 2。
  • 你能不能试着用你的数据取回源,看看它是否能用它。我在上面的代码中评论了您的代码。我总是有演示问题,但我认为它看起来像你想要的(除了垃圾演示)。
  • 感谢您的回答,除了表格下方的额外 li 外,它正在工作。但问题是您的编码方式有点不同。很难理解这就是为什么我希望它在你自己的代码中进行编辑。
【解决方案3】:

您需要更改 autocomplete.php 然后返回所有 3 个值,您可以轻松地在 json 数组中执行此操作 http://php.net/manual/en/function.json-encode.php 然后读取 jquery 脚本中的值。

这是你更新的 JS 脚本

$(document).on('focus','.type',function(){
type = $(this).data('type');
if(type =='vehicle' )autoTypeNo = 1;   
$(this).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url : 'autocomplete.php',
            dataType: "json",
            method: 'post',
            data: {
               name_startsWith: request.term,
              type: type
            },
             success: function( data ) {
                 response( $.map( data, function( item ) {
                    //var code = item.split("|");
                    return {
                        label: item.no + '-' + item.vehicle + '-' + item.type,
                        value: item.vehicle,
                        data : item
                    }
                }));
            }
        });
    },
    autoFocus: true,           
    minLength: 0,
    select: function( event, ui ) {
        //var names = ui.item.data.split("|");                       
        id_arr = $(this).attr('id');
        id = id_arr.split("_");
        $('#no_'+id[1]).val(ui.item.data.no);
        $('#vehicle_'+id[1]).val(ui.item.data.vehicle);
        $('#type_'+id[1]).val(ui.item.data.type);
    }              
 });
});

【讨论】:

    猜你喜欢
    • 2018-01-15
    • 1970-01-01
    • 2020-03-24
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2017-09-04
    相关资源
    最近更新 更多