简体   繁体   中英

how to get value selected in ajax select2 plugin

html

  <label>Customer PO</label>
  <select name="Customer_PO" class="form-control" id="Customer_PO" >
  <option value="" >Customer PO</option>
  </select>
-------------

jquery

$('body').on('click','#Customer_PO',function(e){
id_tosend=$(this).attr("id").toString();
var cust_id=$('#Customer_list').val();
var a=$('#'+id_tosend).select2({
  ajax: {
   url: 'ajax/report/inspection_report.php?cust_id='+cust_id,
   dataType: 'json',
   delay: 500,
   data: function (params) {
            var queryParameters = {
                q: params.term
            }
            return queryParameters;
    },
   processResults: function (data) {
        return {
            results: data
        };
   },
   cache:true
  }

});


a.data('select2').dropdown.$dropdown.addClass("test");
$(this).select2('open');
});

i attach plugin of Select2 .When i submitted data in my form ,it shown me empty value . How i get this value to set selected initial value in ajax method.

you can try this

HTML

<label>Customer PO</label>
<select name="Customer_PO" class="form-control" id="Customer_PO" >
  <option value="" >Customer PO</option>
</select>

JQUERY

$.ajax({
            url: 'url',
            type: 'POST',
            data: { id: id},
            dataType: 'JSON',
            success: function (resp) {
                if(resp.msg == 'Done'){
                    $("#Customer_PO option:selected").removeAttr("selected");

                    $.each(resp.yourListData, function (key, value) {
                      $("#Customer_PO ").append('<option value="'+value.ID+'">'+value.name+'</option>');

                    });
                    $("#Customer_PO ").trigger("change", [true]);
                }
            },
            error: function (e) {
                console.log("Line 1204");
                console.log(e.responseText);
            }
        });    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM