简体   繁体   English

如何在自动完成的jquery中过滤JSON数组并在JavaScript中显示动态值?

[英]How to filter a JSON array in autocomplete jquery and show a dynamic value in JavaScript?

I have successfully implemented the jQuery Autocomplete function in my HTML input filed, it returns an array of JSON objects, what I would like to achieve is the following: 我已经在我的HTML输入字段中成功实现了jQuery Autocomplete函数,它返回了JSON对象数组,我想要实现的目标如下:

  1. click on an item of the dropdown list 单击下拉列表中的一项
  2. filter such item's string to get only the objID string. 过滤此类项目的字符串以仅获取objID字符串。

So, here's my js code: 所以,这是我的js代码:

$.ajax({
   url: "<?php echo $pointerClass ?>.json",
   type: "POST",
   dataType: "JSON"
   }).done(function(data){
      var jsonData = data;
      var arr = new Array();

      var keys = Object.keys(jsonData);
      for(var i=0; i<keys.length; i++){
         var key = keys[i];
         var jString = JSON.stringify(jsonData[key]);
         arr.push(jString);
      }// ./ for
      console.log('arr: ' + arr[0]);

      // autocomplete jquery function
      $( "#ar-type-pointer-objID" ).autocomplete({
         source: arr,
         minLength: 1
      });
    }); 

Here's a screenshot of my drop-down menu: 这是我的下拉菜单的屏幕截图:

在此处输入图片说明

As you can see by the red frame, I need to click on an item and pass only the "objID" value to my input field, so it'll be "qO19zg8mV4" since I'm clicking on the row in the red square. 如红色框所示,我需要单击一个项目,仅将"objID"值传递给我的输入字段,因此,由于我单击红色正方形中的行,因此它将为"qO19zg8mV4"

Here's how my input should look like after clicking on a drop-down's row: 单击下拉列表行后,输入内容如下所示:

在此处输入图片说明

According to the autocomplete documentation, you have two interesting events: select and close. 根据自动完成文档,您有两个有趣的事件:选择和关闭。

select is 选择是

Triggered when an item is selected from the menu. 从菜单中选择一个项目时触发。 The default action is to replace the text field's value with the value of the selected item 默认操作是将文本字段的值替换为所选项目的值

close is: 接近是:

Triggered when the menu is hidden. 菜单隐藏时触发。 Not every close event will be accompanied by a change event. 并非每个关闭事件都会伴随有更改事件。

Select has two parameters: Select具有两个参数:

  1. event 事件
  2. ui UI

ui is an object like this: ui是这样的对象:

item: {
  label: string,
  value: string
}

Not sure where you will get your JSON, probably value, so I assume that...do a console.log to be sure of it! 不知道从哪里可以得到JSON,可能是值,所以我假设...执行console.log来确保它!

You should rewrite with something like 你应该用类似的东西重写

$( "#ar-type-pointer-objID" ).autocomplete({
  source: arr,
  select: function(event, ui) {
    const target = event.target;
    const val = JSON.parse(ui.item.value); // Check if decode is needed or is already passed as an object
    jQuery(target).val(val.ObjID);
    event.preventDefault();
    return false;
  }
});

We prevent the default, because "The default action is to replace the text field's value with the value of the selected item." 我们避免使用默认值,因为“默认操作是将文本字段的值替换为所选项目的值。” and your change in the input field will be lost. 并且您在输入字段中所做的更改将丢失。 You still have to manage some info by yourself but the idea should be enough! 您仍然必须自己管理一些信息,但是这个主意应该足够了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM