Select2动态选择AJAX

时间:2018-08-13 04:25:43

标签: ajax jquery-select2

我有以下AJAX脚本:

$.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.');
                                  }
                                }

                            });

如您所见,在成功事件中,我展示了一些类似于示例在此处建议的内容:https://select2.org/programmatic-control/add-select-clear-items

问题是它不想从被选择的select2(.shipmentSelect)中“选择”该项目。它也不会引发错误,但是什么也没有发生。确实会在控制台中显示成功消息。

响应的格式如下:

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

所以我很好奇是否存在另一种格式化方式,但是我认为问题出在以下几行:

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

我认为除了一般的回复之外,我还需要提供其他内容吗?

谢谢

0 个答案:

没有答案