foxychev
(Foxychev)
January 4, 2016, 9:53am
1
Please I got a problem on Yii2:
I want to insert value into a textInput field based on changes in the selected option from a dropdownlist. Here is my code:
<?= $form->field($model, 'course_taken')->dropDownList(ArrayHelper::map(Courses::find()->all(),'course_code','course_code')) ?>
<?= $form->field($model, 'course_details')->textInput(['maxlength' => true]) ?>
So when the user selects a course from the dropdownlist, the textInput will display the course details of the selected row. Both course_taken and course_details are columns from the same database table.
Thanks in advance.
ajith
(Ajith Tejas)
January 4, 2016, 11:31am
2
please try this link
http://stackoverflow.com/questions/31064368/update-textinput-value-by-selecting-a-dropdown-list-in-yii-2
foxychev
(Foxychev)
January 5, 2016, 7:08pm
3
Thanks.
But that did not work. My case is slightly different. Any more help, please?
selvakumar
(Selvakumar Kaliyappan)
January 6, 2016, 5:52am
4
Hi foxychev
Try this,
<?= $form->field($model, 'course_taken')->dropDownList(
ArrayHelper::map(Courses::find()->all(),'course_code','course_code'),
[
'prompt'=>'--Select--',
'onchange'=>'$("#model-course_details").val($("#model-course_taken option:selected").text());'
])
?>
<?= $form->field($model, 'course_details')->textInput(['maxlength' => true]) ?>
foxychev
(Foxychev)
January 6, 2016, 9:15am
5
Tried it but nothing happened. The course_details textinput remained blank after making selection…
selvakumar
(Selvakumar Kaliyappan)
January 7, 2016, 6:14am
6
<?= $form->field($model, 'course_taken')->dropDownList(
ArrayHelper::map(Courses::find()->all(),'course_code','course_code'),
[
'prompt'=>'--Select--',
'onchange'=>'$("#course_details").val($("#course_taken option:selected").text());',
'id'=>'course_taken',
])
?>
<?= $form->field($model, 'course_details')->textInput(['maxlength' => true, 'id'=>'course_details']) ?>
Set the field ID’s manually and check it.
cesar.yii
(Cesarfpmx)
April 12, 2016, 10:07pm
7
Did that work? I’m in the same situation… I’ll try what Selvakumar suggests…
cesar.yii
(Cesarfpmx)
April 12, 2016, 10:29pm
8
My case is kind of different how ever I tried this option to see the behavior and It didn’t work. I coded this:
<?=$form->field($ordenProducto, 'id_producto')->dropDownList(ArrayHelper::map($catProductos,'id','descripcion'),
['prompt'=>'-Choose a option-',
'onchange'=>'if($(this).val() == 1){
$("#precio_unitario").val("333"); }'
]); ?>
<?= $form->field($catProductos, 'precio_unitario')->textInput(['maxlength' => true, 'id'=>'#precio_unitario']) ?>
Is there something wrong?