通过在Yii2中使用kartik select2插件,我尝试对国家和州进行依赖下拉列表。

国家和州的表单字段:

$url = yii\helpers\Url::toRoute('op-client/lists');

$this->registerJs($this->render('script.js'), \yii\web\VIEW::POS_READY);


$form->field($model, 'country_id')->widget(Select2::classname(), [
                                    'data' => $countryData,
                                    'language' => 'en',
                                    'options' => ['placeholder' => 'Select'],
                                    'pluginOptions' => [
                                        'allowClear' => true,
                                    ],
                                    'pluginEvents' =>
                                    [
                                        'change' => 'function()
                                          {
                                              getstate
                                              (
                                                $("#select2-opclient-country_id-container").val(),
                                                 "'.$url.'"
                                              )
                                          }',
                                    ],
                                ]).'


                                $form->field($model, 'states_id')->widget(Select2::classname(), [
                                    'data' => $statesData,
                                    'language' => 'en',
                                    'options' => ['placeholder' => 'Select'],
                                    'pluginOptions' => [
                                        'allowClear' => true,
                                    ],

                                ]).'


Script.js

function getstate($countryid,url)
{
    //console.log(startdate + enddate);
 var csrfToken = $('meta[name="csrf-token"]').attr("content");

    $.ajax({
        type:"POST",
        cache:false,
        url:url,
        data:{countryid:countryid, _crsf:csrfToken},
        success:function(data){
            $("#select2-opclient-states_id-container").val(data);
        },
    })
}


控制器:

public function actionLists()
    {
        $request = Yii::$app->request;

        $country = $request->post('countryid');

        $countStates = OpStates::find()
                        ->where(['country_id' => $country])
                        ->count();

        $states = OpStates::find()
                        ->where(['country_id' =>$country])
                        ->all();

        if($countStates > 0)
        {
            foreach($states as $state){
                echo "<option value='".$state->id."'>".$state->state_name."</option>";
            }
        }
        else
        {
            echo "<option></option>";
        }
    }


当我运行该程序时,它显示错误“ Uncaught ReferenceError:未定义countryid”。
但是我以为我已经把那个乡下人带进去了?我在哪里做错了?

任何帮助/建议将不胜感激。谢谢你

最佳答案

请检查以下代码,我认为您在country_id变量名称中没有犯错。

public function actionLists()
    {
        $request = Yii::$app->request;

        $country = $request->post('country_id');

        $countStates = OpStates::find()
                        ->where(['country_id' => $country])
                        ->count();

        $states = OpStates::find()
                        ->where(['country_id' =>$country])
                        ->all();

        if($countStates > 0)
        {
            foreach($states as $state){
                echo "<option value='".$state->id."'>".$state->state_name."</option>";
            }
        }
        else
        {
            echo "<option></option>";
        }
    }


和这里

function getstate(countryid,url)
{
    //console.log(startdate + enddate);
 var csrfToken = $('meta[name="csrf-token"]').attr("content");

    $.ajax({
        type:"POST",
        cache:false,
        url:url,
        data:{countryid:countryid, _crsf:csrfToken},
        success:function(data){
            $("#select2-opclient-states_id-container").val(data);
        },
    })
}


它将解决您的问题。

10-01 15:15