在 Yii2 中保存 select2 下拉列表中的数据,而不使用活动形式


Saving data from select2 dropdown in Yii2 without active form

我有一个模态窗口,其中包含一个下拉列表,询问关闭某些内容的原因。

单击保存,我希望在 select2 中选择的信息像以活动形式一样更新模型......

下面是模式窗口的代码:

<div class="modal fade close-modal" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
              <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="myModalLabel">Reason for closure</h4>
              </div>
            <div class="modal-body">
                <?php echo Select2::widget([
                    'model' => $model,
                    'attribute' => 'opportunity_closure_id',
                    'name' => 'closure-reason',
                    'options' => [
                        'limit' => 10,
                    ],
                    'addon'=> [
                    ],
                    'pluginOptions' => [
                        'allowClear' => true,
                        'ajax' => [
                            'url' => 'Yii::$app->urlManager->createUrl('opportunity/closuredatalist'),
                            'dataType' => 'json',
                            'data' => new JsExpression($dataExp),
                            'results' => new JsExpression($dataResults),
                        ],
                        'initSelection' => new JsExpression($initScriptClosure),
                        'multiple' => false
                    ]
                ])?>            
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <?php echo 'yii'helpers'Html::a('<span class=""></span> Save', ['opportunity/close','id' => $model->id], ['class' => 'btn btn-primary'])?>
            </div>
        </div>
    </div>
</div>

关于我该如何做到这一点的任何想法?

尝试将type: "POST"添加到 AJAX 调用

我在下面有 ajax 调用工作,

        $.ajax({
        url: eduLevelCreateURL,
        cache: false,
        data: {"name":level.name,"shdesc":level.shdesc},
        type: "POST",
        timeout: 10000,
        dataType: "json",
        beforeSend: function (){
            $("#progresBarUpload").addClass("active");
            console.log(eduLevelCreateURL + " " + dataJ);
        },
        success: function (result) {
            console.log(result);
            var chtml = $("#person-educationid").html();
            chtml+=result.data;
            $("#person-educationid").html(chtml);
            $('#newCat').modal('hide');
            var value = result.value;
            $("#person-educationid option[value='" + value + "']").attr("selected", "selected");
        },
        error: function (msg) {
            alert("Error'n" + msg.toString());
        }
    });