选择某个答案时显示不同的活动字段,Yii2

时间:2015-12-15 19:52:51

标签: jquery yii2 yii2-basic-app

我有一个带有radioList的活动表单。当选择“其他”选项时,我想使用jquery显示隐藏字段。 出于某种原因,我尝试过的东西不起作用。 我的表格:

<?= $form->field($model, 'program_motivation', [
        'inputOptions' =>[
            'id' => 'motiv'
        ]
    ])->radioList(array('1'=>'money','2'=>'content','3'=>'other')); ?>


 <?= $form->field($model, 'otherMotiv',['inputOptions' => [
        'id' => 'otherM'
]]))->hiddenInput()->textInput(['maxlength' => 10])->label('insert other option')?>

我的javascript:

var program = $('#motiv').val();
if (program == 'other'){
    $('#otherM').show();
}

2 个答案:

答案 0 :(得分:3)

1).val()表示值(在您的情况下,您有1,2或3个值)。 if (program == 'other')在这里比较文字,而不是价值

2)

$('input[name="MyModel[program_motivation]"]').change(function() {
   if (this.value == 3)
      $('#otherM').show();
});

3)删除hiddenInput()并设置选项:

<?= $form->field($model, 'otherMotiv',[
    'options' => [
        'class' => 'form-group',
        'style' => 'display: ' . ($model->program_motivation == 3) ? 'block' : 'none')
    ],
    'inputOptions' => [
        'id' => 'otherM'
    ]]))->textInput(['maxlength' => 10])->label('insert other option')?>

答案 1 :(得分:0)

我认为你应该使用普通的textInput而不是隐藏的输入,但是使用css显示或设置为none或者将visibility设置为hidden,或者将textInput的容器div设置为<% library(leaflet) m <- leaflet() m <- addTiles(m) m <- addMarkers(m, lng=174.768, lat=-36.852, popup="The birthplace of R") m #Does not show anything saveWidget(m, file = outPath, selfcontained = FALSE, libdir ='leafletwidget_libs') #work but not proper %> 或{{1}然后当ypu需要show或textInput或容器div

首先尝试输入css display:hidden;

visibilty: hidden;

display:none;