2017-02-18 4 views
2

私はselect2プラグインに新しくなりました。私の問題は 私はキーワードphpを使用してクエリを実行したときに、jsonとして対応するデータを返すジョブ検索オプションをWebページに追加したいと思います。たとえば、ユーザーがjavaを入力すると、java、javascript、java.netなどの可能性の高い単語が返され、ユーザーは表示されたリストから1つ以上の項目を選択できます。 私がしたが、何の選択オプションphpとajaxでselect2プラグインを使用するには?

スクリプト

$(".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;"> 
+0

何を試してみましたか?今あなたが持っているのはその唯一のものですか? – RohitS

+0

[フィドル](http://jsfiddle.net/deep3015/x62Lwgrt/)いくつかのアイデアを与える –

答えて

0

は私がPOを持っている完全なソリューションを下回る

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

    }, 


}); 
}); 

を探すありません動的なデータのための他の場所へのinted URL ..それに応じて変更を加えてください

関連する問題