簡體   English   中英

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

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

我是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;">

查找以下完整的解決方案

     $(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指向動態數據的其他位置。請相應地進行更改

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM