通过选择yii 2中的下拉列表来更新textinput值

问题描述:

I'm very new to Yii and I'm trying to update a value of an textinput , depending on the value selected on a dropdown list. in my view I have this inside my active form

<?= $form->field($model, 'quantity')->textInput(['readonly' => !$model->isNewRecord]) ?>//this is an integer

<?= $form->field($model, 'allowquantity')->dropdownlist(['yes' =>'yes','no'=>'no']) ?>

I want to be able to change the value of the field quantity, depending on the selected value in dropdownlist. example, if i choose yes in the dropdown, it will retain the value in the quantity field. I choose no , it will turn the value to 0. this will happen in my update form so that i can update the values in the database. Any help will be much appreciated

我是Yii的新手,我正在尝试更新textinput的值,具体取决于值 在下拉列表中选择。 在我看来,我在我的活动表格中有这个 p> &lt;?= $ form-&gt;字段($ model,'quantity') - &gt; textInput(['readonly '=&gt;!$ model-&gt; isNewRecord])?&gt; //这是一个整数 &lt;?= $ form-&gt;字段($ model,'allowquantity') - &gt; dropdownlist([' 是'=&gt;'是','不'=&gt;'不'])?&gt; code> pre>

我希望能够更改值 字段数量,取决于下拉列表中的选定值。 例如,如果我在下拉列表中选择“是”,它将保留数量字段中的值。 我选择不,它会将值转为0.这将在我的更新表单中发生,以便我可以更新数据库中的值。 任何帮助将不胜感激 p> div>

Change your dropdown , make sure #quantity is the id of quantity textbox

echo $form->field($model, 'allowquantity')->dropDownList(['yes' =>'yes','no'=>'no'], 
             ['prompt'=>'-Choose a option-',
              'onchange'=>'if($(this).val() == "no"){
                 $("#quantity").val("0"); }'
              ]);