How to set selected value of jquery select2?

后端 未结 28 1354
野趣味
野趣味 2020-11-30 00:00

This belong to codes prior to select2 version 4

I have a simple code of select2 that get data from ajax



        
28条回答
  •  既然无缘
    2020-11-30 00:46

    I think you need the initSelection function

    $("#programid").select2({
      placeholder: "Select a Program",
      allowClear: true,
      minimumInputLength: 3,
      ajax: {
        url: "ajax.php",
        dataType: 'json',
        quietMillis: 200,
        data: function (term, page) {
          return {
            term: term, //search term
            flag: 'selectprogram',
            page: page // page number
          };
        },
        results: function (data) {
          return {results: data};
        }
      },
      initSelection: function (element, callback) {
        var id = $(element).val();
        if (id !== "") {
          $.ajax("ajax.php/get_where", {
            data: {programid: id},
            dataType: "json"
          }).done(function (data) {
            $.each(data, function (i, value) {
              callback({"text": value.text, "id": value.id});
            });
            ;
          });
        }
      },
      dropdownCssClass: "bigdrop",
      escapeMarkup: function (m) { return m; }
    });
    

提交回复
热议问题