简体   繁体   English

如何在PHP和Ajax中使用select2插件?

[英]How to use select2 plugin with php and ajax?

I am new to select2 plugin, my problem is, I want to put a job search option to my web page that is when user queries with keyword php then it will return corresponding data as json. 我是select2插件的新手,我的问题是,我想在我的网页上放置一个求职选项,即当用户使用关键字php查询时,它将返回对应的数据作为json。 For example if user enters java then it will return most possible words like java, javascript, java.net and user can pick up one or more item from the list displayed. 例如,如果用户输入java,则它将返回大多数可能的单词,例如java,javascript,java.net,并且用户可以从显示的列表中选取一项或多项。 i did but there is no select option 我做到了,但是没有选择选项

script 脚本

$(".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 HTML

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

Find below Complete solution 查找以下完整的解决方案

     $(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
                    }
                })
            };
      }

    },


});
});

I have pointed url to some other location for dynamic data..Please make changes accordingly 我已将url指向动态数据的其他位置。请相应地进行更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM