【问题标题】:How to create jquery array from ajax data?如何从 ajax 数据创建 jquery 数组?
【发布时间】:2019-06-05 05:08:23
【问题描述】:

我想从 ajax 响应创建数组。我从 ajax 获取数组,我必须为芯片创建动态数据 materializecss autocomplete

response from ajax

var tag = '';
$(document).on('keyup', '.tags', function() {

    var _this = $(this).val();

    if(_this.length > 1) {

        $.ajax({
            url: '/story/searchTags',
            type: 'POST',
            dataType: 'JSON',
            data: {
                tag: _this
            },
            success: function(tags) {
                tag = tags;
            }
        });

    }

});

$('.chips-placeholder').chips({
    placeholder: lang_('Your tag'),
    secondaryPlaceholder: '+ tag',
    autocompleteOptions: {
        // I need data like this from response
        data: {
           'Google', null
           'Googol', null
        },
        limit: 5,
        minLength: 2
    }
});
<div class="chips chips-placeholder chips-autocomplete input-field" style="background:#fff">
   <input class="input tags" placeholder="tag" autocomplete="off" />
</div>

that's my code

最后,感谢@Vel 找到正确答案。 jsfiddle - work code

【问题讨论】:

  • 你的代码和你的解释与data不同
  • @M.Hemant 哪里不一样?

标签: php jquery arrays ajax


【解决方案1】:

你需要这样做。

var tag = '';
$(document).on('keyup', '.tags', function() {

    var _this = $(this).val();

    if(_this.length > 1) {

    /*$.ajax({
        url: '/story/searchTags',
        type: 'POST',
        dataType: 'JSON',
        data: {
        tag: _this
        },
        success: function(tags) {
        tag = tags;
        }
    });*/

    // ['google', 'googol'] <- this array I take from ajax
    tag = ['google', 'googol'];
    var obj = {};       

    $(tag).each(function (index, value) {       
        obj[value] = null;          
    });

    $('.chips').chips({
      secondaryPlaceholder: '+ tag',
      autocompleteOptions: {              
      data:obj,
      }
      });
    }

});

fiddle link

【讨论】:

  • 没有错误,它根本不起作用。也许芯片功能在其他功能中不起作用。我更新了问题,看看
  • @ГаниШахмурат,我已经更新了小提琴。请检查。它工作正常。
猜你喜欢
  • 1970-01-01
  • 2012-11-22
  • 1970-01-01
  • 2021-02-21
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 2018-11-08
  • 2017-02-18
相关资源
最近更新 更多