autosearch建议不起作用

时间:2016-01-08 03:59:24

标签: javascript jquery html arrays json

我正在根据 AutoComplete plugin 开展自动填充建议。我正在使用html js css。当我使用var currency = [“a”,“b”,“a1”,“a2”,“a3”]时它工作;但是当我使用 JSON RESPONSE 时它不起作用。当我在输入字段上输入时,它会给出未捕获的TypeError:无法读取属性'toLowerCase'的未定义错误但是如果使用它会起作用 var currency = [“a”,“b”,“a1”,“a2”,“a3”];

/**js for invoking backend service which is in json format**/

function wlCommonInit() {
  var currencies = [];

  function empautos(result) {
    WL.Logger.debug("feed retrieve successsss");
    es1(result.responseJSON.Envelope.Body.processResponse.EmpList);
  }

  function es1(result) {
    WL.Logger.debug("Checking here");
    for (var i = 0; i <= result.length; i++) {
      currencies[i] = result[i].ENAME;
      WL.Logger.debug(currencies);
      //console
      /** ["1, Approver","1222430837 lastname, Mr. 1222430837 firstname",
      "A Bakker","A Chunekar","A Gopalan","AGD, Mr. APPROVER","APPROVER, JOURNAL","Aachen, Torben","Aafjes , B"] **/
    }
  }

  $('#autocomplete').autocomplete({
    lookup: currencies
  });


}

<div id="searchfield">
     <form>
        <input type="text" name="currency" class="biginput" id="autocomplete"/>
     </form>
</div>

1 个答案:

答案 0 :(得分:0)

根据我对插件文档的理解,您必须将数据转换为[{data:&#39;&#39;,value:&#39;&#39;}]格式。基本上,一组对象和每个对象应该有键datavalue

所以我操纵你的数据如下

var data = [
  "1, Approver",
  "1222430837 lastname, Mr. 1222430837 firstname",
  "A Bakker",
  "A Chunekar",
  "A Gopalan",
  "AGD, Mr. APPROVER",
  "APPROVER, JOURNAL",
  "Aachen, Torben",
  "Aafjes , B"
];

data = data.map(function(item) {
    return {data: item, value: item};
});

<强> Here is the working code.