簡體   English   中英

如何獲得在ajax select2插件中選擇的值

[英]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');
});

我附加了Select2插件。當我以表單提交數據時,它向我顯示了空值。 我如何獲取此值以在ajax方法中設置選定的初始值。

你可以試試這個

的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);
            }
        });    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM