将json对象传递给数组-自动完成 [英] pass json object to array - autocomplete

查看:97
本文介绍了将json对象传递给数组-自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件.

location.php, that outputs this:

[["javascript"],["PHP"]] 

并在另一个文件中:

<script type="text/javascript">
$.getJSON('location.php', function(data) {
      var sampleTags = [];

      $.each(data, function(key, val) {
         sampleTags.push(val);
         });

         alert(sampleTags); // show javascript, php


        //-------------------------------
        // Preloading data in markup
        //-------------------------------
        $('#myULTags').tagit({
            availableTags : sampleTags, // this param is of course optional. it's for autocomplete.
            // configure the name of the input field (will be submitted with form), default: item[tags]
            itemName : 'item',
            fieldName : 'tags'
        });
    });
</script>

自动完成不起作用.为什么?

the auto complete doesn't work. Why ?

如果我使用:

var sampleTags = [ 'javascript', 'php'];

一切正常,但是使用json时,自动完成功能根本无法正常工作.

all works well, but with json the autocomplete simply doesn't work.

推荐答案

$.each(data, function(key, val) {
  sampleTags.push(val[0]);
});

应将[["foo"],["bar"]]减少为["foo","bar"]

should reduce [["foo"],["bar"]] to ["foo", "bar"]

这篇关于将json对象传递给数组-自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆