【问题标题】:How to use select2 plugin with php and ajax?如何在 php 和 ajax 中使用 select2 插件?
【发布时间】:2017-02-18 06:55:40
【问题描述】:

我是 select2 插件的新手,我的问题是, 我想在我的网页上添加一个工作搜索选项,即当用户使用关键字 php 进行查询时,它将以 json 格式返回相应的数据。例如,如果用户输入 java,那么它将返回大多数可能的单词,例如 java、javascript、java.net,并且用户可以从显示的列表中选择一项或多项。 我做了,但没有选择选项

脚本

$(".load").select2({
 minimumInputLength: 2,
    ajax: {
          url: "http://localhost/testserver/Test",
      dataType: 'json',
       type: "post",

      data: function (term, page) {
        return {
          q: term
        };
      },
      processResults: function (data, page) {
      console.log(data);
        return {
                results: $.map(data, function (item) {
                    return {
                        text: item.Name,


                    }
                })
            };
      }

    },


});

html

<select class="load" style="width:400px;">

【问题讨论】:

  • 显示您尝试过的所有内容..?这是你现在唯一拥有的东西吗?
  • fiddle 给点意见

标签: javascript jquery ajax select2


【解决方案1】:

在下面找到完整的解决方案

     $(document).ready(function() {          
            $(".load").select2({
 minimumInputLength: 2,
    ajax: {
          url: "http://ip.jsontest.com/",
      dataType: 'json',
       type: "post",

      data: function (term, page) {
        return {
          q: term
        };
      },
      processResults: function (data, page) {

        return {
                results: $.map(data, function (item) {  console.log(item);
                    return {
                        text: item
                    }
                })
            };
      }

    },


});
});

我已将 url 指向其他位置以获取动态数据。请进行相应更改

【讨论】:

    猜你喜欢
    • 2019-09-01
    • 2014-07-31
    • 2015-09-23
    • 1970-01-01
    • 2018-01-31
    • 2018-01-29
    • 2021-08-27
    • 1970-01-01
    • 2013-01-20
    相关资源
    最近更新 更多