我的表单中有一个dropDownList,如下所示:
<?= $form->field($model, 'object_typeID')->dropDownList(
ArrayHelper::map(ObjectTypes::find()->all(),'object_typeID','title'),
[
'prompt' => 'Choose Object type',
]
) ?>
我希望在提交表单之前根据选定的dropDownList值加载其他表单元素,这样我应该在用户选择它时立即选择值。如何在提交之前获取dropDownList选择的值?
答案 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)
答案 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);
});