Select2 initSelection

前端 未结 2 1467
栀梦
栀梦 2021-01-14 02:16

I have a problem to set the method initSelection with an ajax call, I returns \"undefined\". I checked and the ajax call returns the correct result .. I donot understand the

2条回答
  •  感动是毒
    2021-01-14 02:46

    Pay attention, the data you send to the callback function, needs to be an object, with an id and text attributes.

    This is what worked for me:

    initSelection: function(element, callback) {
        var id;
        id = $(element).val();
        if (id !== "") {
          return $.ajax({
            url: url,
            type: "POST",
            dataType: "json",
            data: {
              id: id
            }
          }).done(function(data) {
            var results;
            results = [];
            results.push({
              id: data.id,
              text: data.name
            });
            callback(results[0]);
          });
        }
      }
    

提交回复
热议问题