如何在Yii2中提交之前获取dropDownList选择的值?

时间:2016-09-26 14:26:04

标签: yii2

我的表单中有一个dropDownList,如下所示:

<?= $form->field($model, 'object_typeID')->dropDownList(
                ArrayHelper::map(ObjectTypes::find()->all(),'object_typeID','title'),
                [
                    'prompt' => 'Choose Object type',
                ]
            ) ?>

我希望在提交表单之前根据选定的dropDownList值加载其他表单元素,这样我应该在用户选择它时立即选择值。如何在提交之前获取dropDownList选择的值?

3 个答案:

答案 0 :(得分:0)

$model->object_typeID设置为下拉值的任意值。这是一个例子:

$model = Model::findOne(3);
echo $model->object_typeID;
// let's say it prints 3
$dropdown = [1 => 'Ejaz', 2 => 'Nizar', 3 => 'Saleem'];
$form->field($model, 'object_typeID')->dropDownList(
            $dropdown,
            [
                'prompt' => 'Choose Object type',
            ]
        )

 // the above code will select Saleem by default.

答案 1 :(得分:0)

你是说这个意思吗?从dropDownList中选择值并将值赋给另一个字段? https://www.youtube.com/watch?v=hZ-6huMYxBc&index=24&list=PLRd0zhQj3CBmusDbBzFgg3H20VxLx2mkF

答案 2 :(得分:0)

你需要使用jquery。

Step 1: Add an id(say 'listID') to your current field 
Step 2: Add a class(say 'customClassName hide') to next field
Step 3: Find out the value of the selected item from the list using jquery and add/remove class as below :

$('#listID').change(function () {
    var formValue = $(this).val();
    var RequiredValue = '0'; // field to be shown on this value
    if(formValue == RequiredValue){
        newClass = 'show customClassName';
    }else{
        newClass = 'hide customClassName';
    }       
    $('.customClassName').removeClass().addClass(newClass);
});