【问题标题】:how to get value selected in ajax select2 plugin如何获取在ajax select2插件中选择的值
【发布时间】:2018-01-29 11:00:15
【问题描述】:

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 方法中设置选定的初始值。

【问题讨论】:

标签: javascript jquery ajax jquery-selectors jquery-select2


【解决方案1】:

你可以试试这个

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

【讨论】:

  • 对不起,我迟到了。 yourListData 意味着,从 ajax 调用返回的数据。检查我编辑的答案
  • 我使用了 select2 插件,如果您知道,请解决这个问题...
猜你喜欢
  • 2016-03-31
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-23
  • 2013-11-23
相关资源
最近更新 更多