javascript - 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 submitted data in form ,it shown me empty value . how value 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);             }         });     

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -