How to get Selected Text from select2 when using <input>

前端 未结 9 1247
心在旅途
心在旅途 2020-12-12 23:38

I am using the select2 control, loading data via ajax. This requires the use of the tag.

Now, I want to retrieve the selecte

9条回答
  •  情话喂你
    2020-12-12 23:58

    Again I suggest Simple and Easy

    Its Working Perfect with ajax when user search and select it saves the selected information via ajax

     $("#vendor-brands").select2({
       ajax: {
       url:site_url('general/get_brand_ajax_json'),
      dataType: 'json',
      delay: 250,
      data: function (params) {
      return {
        q: params.term, // search term
        page: params.page
      };
    },
    processResults: function (data, params) {
      // parse the results into the format expected by Select2
      // since we are using custom formatting functions we do not need to
      // alter the remote JSON data, except to indicate that infinite
      // scrolling can be used
      params.page = params.page || 1;
    
      return {
        results: data,
        pagination: {
          more: (params.page * 30) < data.total_count
        }
      };
    },
    cache: true
    },
    escapeMarkup: function (markup) { return markup; }, // let our custom    formatter work
    minimumInputLength: 1,
    }).on("change", function(e) {
    
    
      var lastValue = $("#vendor-brands option:last-child").val();
      var lastText = $("#vendor-brands option:last-child").text();
    
      alert(lastValue+' '+lastText);
     });
    

提交回复
热议问题