自动完成从json数组获取关注值

时间:2016-06-16 17:58:48

标签: jquery json wordpress-json-api

  jQuery(function() {
   var ingrident_array_json = [{"id_product":"45","name":"Acetyl Hexapeptide-8\ufeff (Argireline)","unit_price":"19.4500000000","usage_percent_lo":"3.00","usage_percent_hi":"10.00","usage_percent_best":"10.00"},{"id_product":"46","name":"Ceramide Complex (CeraTouch\u2122)","unit_price":"23.6125000000","usage_percent_lo":"3.00","usage_percent_hi":"10.00","usage_percent_best":"5.00"}];
  $("input:text[id^='ingredient']").live("focus.autocomplete", null, function () {
 $(this).autocomplete({
    autoFocus: true,
    source: projects,
    focus: function( event, ui ) {
        $( "#autocomplete" ).val( ui.item.name );
        return false;
      },
      select: function( event, ui ) {
        $(this).val( ui.item.name );
        $(this).attr("data-value",ui.item.unit_price);
        $(this).closest('.extra_fileds').find('.product_id_public').val(ui.item.unit_price);
        return false;
      }
    })
    });

  });

我有ingrident_array_json json数组,每个记录中有六个元素,我想在autocomplete populate中输入名字,在隐藏文件中需要其他记录。可能吗?现在什么都没有。如有任何帮助,将不胜感激

1 个答案:

答案 0 :(得分:0)

首先,不推荐使用live方法。在项目中使用'on()'。

其次使用如下:

var arr = $.map(ingrident_array_json, function(el) { return el });
$(function () {
    $('#ingredient').autocomplete({
        minLength: 3,
        source: arr,
        select: function (event, ui) {
           $('#ingredient').val( ui.item.name );
           $('#ingredient').attr("data-value",ui.item.unit_price);
           $('#ingredient').closest('.extra_fileds').find('.product_id_public').val(ui.item.unit_price);
           return false;
        }
    })
    .autocomplete().data("uiAutocomplete")._renderItem = function (ul, item) {
        return $("<li>")
          .append("<a>" + item.name + "</a>")
          .appendTo(ul);
    };
});

希望这有帮助。