2017-05-19 13 views
0

YII2は、二つのフィールドのドロップダウンリスト(relationship_status)およびテキスト入力フィールド(spouse_name)を有しています。結婚した人がドロップダウンリストで選択された場合、配偶者名のテキストフィールドのみが表示されます。Yii2ドロップダウン値依存の表示/非表示テキストフィールド

+1

はそうドロップダウン値に基づいて/非表示のフィールドを表示するにはJavaScript/jQueryのを使用し 。 – ArtOsi

答えて

0

私はこの方法のようにそれを作るしようとしていました。

<?php $form = ActiveForm::begin(); ?> 

<div id="marriage"> 
    <?= $form->field($model, 'marriage_status')->dropDownList([ 'married' => 'Married', 'unmarried' => 'Unmarried' ], ['prompt' => 
'結婚ステータス'])>

フィールド($モデル、 'spouse_name') - ??>にtextInput([ 'MAXLENGTH' =>真])> isNewRecord? 'Create': 'Update'、['class' => $ model-> isNewRecord? 'BTN BTN-成功':? 'BTN BTN-主'])>

<?php ActiveForm::end(); ?> 

$(document).ready(function(){  $("#spouse").hide(); 
$('#marriage').on('change', function() { 
    if ($(event.target).val() == 'married') 
    { 
    $("#spouse").show(); 
    } 
    else{ 
     $("#spouse").hide(); 
    }   
}); }); 
関連する問題