weixin_33682790 2018-08-13 04:25 采纳率: 0%
浏览 40

Select2动态选择AJAX

I have the following AJAX script:

$.ajax({
                                type:'POST',
                                url: '/carrier/manifests/storeManifestShipments',
                                data: {
                                    proNumber: proNumber,
                                    bolNumber: bolNumber,
                                    poNumber: poNumber,
                                    SpecInst1: SpecInst1,
                                    SpecInst2: SpecInst2,
                                    SpecInst3: SpecInst3,
                                    billTo: billTo,
                                    originName: originName,
                                    originAddress1: originAddress1,
                                    originAddress2: originAddress2,
                                    originCity: originCity,
                                    originState: originState,
                                    originZip: originZip,
                                    consigneeName: consigneeName,
                                    consigneeAddress1: consigneeAddress1,
                                    consigneeAddress2: consigneeAddress2,
                                    consigneeCity: consigneeCity,
                                    consigneeState: consigneeState,
                                    consigneeZip: consigneeZip,
                                    shipmentProjectedDate: shipmentProjectedDate,
                                    shipmentWeight: shipmentWeight,
                                    shipmentPieceCount: shipmentPieceCount,
                                    createdBy: '{{Auth::user()->id}}',
                                    _token: $('input[name=_token]').val(),
                                    dataType: 'json',
                                },
                                success: function(response) {
                                    if(response !== undefined) {

                                       $('#createShipment').modal('hide');
                                       var shipmentSelect = $('.shipmentSelect');
                                       var option = new Option(response.pro_number, response.id, true, true);

                      shipmentSelect.append(option).trigger('change');

                      // manually trigger the `select2:select` event
                      shipmentSelect.trigger({
                        type: 'select2:select',
                        params: {
                          data: response
                        }
                      });
                                            console.log('success');
                                    }else{
                                            console.log('failed');
                                            console.log(response);

                                    }
                                },
                                error: function (jqXHR, textStatus, errorThrown) {
                                  if (jqXHR.status == 500) {
                                      toastr.error('Validation error - Make sure all required fields are filled!', 'Error Alert', {timeOut: 5000});
                                      console.log('Internal error: ' + jqXHR.responseText);
                                  } else if (jqXHR.status == 422) {
                                      toastr.error('Validation error - Make sure all required fields are filled!', 'Error Alert', {timeOut: 5000});
                                      console.log('Internal error: ' + jqXHR.responseText);
                                  } else {
                                      console.log('Unexpected error.');
                                  }
                                }

                            });

As you can see, within the success event I lay out something similar to how the example suggests it here: https://select2.org/programmatic-control/add-select-clear-items.

The problem is that it doesn't want to "select" the item from the select2 that is referenced (.shipmentSelect). It doesn't throw an error either, but nothing really happens. It does print the success message in the console.

The response is formatted as such:

id: 1
pro_number: 1234
name: person1
.....

So I'm curious if there is a different way of formatting it, but I believe the issue comes from the following lines:

  shipmentSelect.trigger({
                        type: 'select2:select',
                        params: {
                          data: response
                        }
                      });

I believe I need to provide maybe something else besides the general response?

Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 csmar数据进行spss描述性统计分析
    • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
    • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
    • ¥15 运动想象脑电信号数据集.vhdr
    • ¥15 三因素重复测量数据R语句编写,不存在交互作用
    • ¥15 微信会员卡等级和折扣规则
    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?