2016-09-26 78 views
0

我有一個像這樣在我的形式下拉列表:如何在Yii2提交之前獲取dropDownList選擇的值?

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

,我想加載基於選定的下拉列表值其他表單元素進行提交,我應該馬上選擇的值當用戶選擇它的形式之前。如何在提交之前獲取dropDownList選定的值?

回答

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. 
+0

我dropDownList填充另一個數據庫表!我需要一個基於用戶選擇的dropDownList值加載其他表單元素的Ajax! –

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); 
}); 
+0

謝謝,但我有另一個dp表中的表單元素應該有條件地(基於用戶選擇)打印在窗體中,我不能使用顯示/隱藏課程!其他解決方案? –

+0

我的主要問題是第3步:找出所選值的值 :D –

相關問題