Yii2-如何用下拉列表隐藏块

时间:2018-07-31 16:46:30

标签: jquery html css yii2

我正在尝试使用Dropdownlist更改事件在Yii2项目中隐藏和显示div,我已经尝试了这段代码,但它似乎对我不起作用。当我单击study_centre_id Dropdownlist onchange事件时,它什么也不做。如果有人可以指出我在哪里犯错,我将不胜感激。预先感谢。

Dropdownlist

控制器

public function actionIndex()
{
    $model = new Programme();       
    $model->scenario = 'import-programme';

    return $this->render('index', [
        'model' => $model,
    ]);
}

查看

<div class="box-info box box-solid view-item col-xs-12 col-lg-12 no-padding">
	<div class="box-header with-border">
		<h3 class="box-title"><i class="fa fa-file-excel-o"></i> <?php echo Yii::t('app', 'Select File'); ?></h3>
	</div><!--./box-header-->
<div id="showProgramImport" style="display:none">        
	<div class="box-body">
		<div class="row">
			<div class="col-sm-12 col-xs-12">
				<?= $form->field($model, 'importFile')->fileInput(['title' => Yii::t('app', 'Browse Excel File')])->label(false) ?>
			</div>
		</div>
		<div class="row">
			<div class="col-sm-12 col-xs-12">
				<div class="callout callout-info">
					<h4><?php echo Yii::t('app', 'You must have to follow the following instruction at the time of importing data'); ?></h4>
					<ol>
						<li><b><?php echo Yii::t('app', 'The field with red color are the required field cannot be blank.'); ?></b></li>
						<li><?php echo Yii::t('app', 'The file must be CSV format.'); ?></li>
					</ol>
					<h5><?php echo Yii::t('app', 'Download the sample format of Excel sheet.'); ?> <b><?= Html::a(Yii::t('app', 'Download'), ['download-file', 'id' => 'SSF']) ?></b></h5>
				</div><!--./callout-->
			</div><!--./col-->
		</div><!--./row-->
	</div><!--./box-body-->
	<div class="box-footer">
		<?= Html::submitButton('<i class="fa fa-upload"></i>'.Yii::t('app', ' Import'), ['class' => 'btn btn-primary']) ?>
	</div>
</div>     
<div id="showProgram" style="display:block">        
	<div class="box-body">
		<div class="row">
			<div class="col-sm-12 col-xs-12">
				<div class="callout callout-danger">
					<h4><?php echo Yii::t('app', 'You Need to Select a Study Centre.'); ?></h4>
				</div><!--./callout-->
			</div><!--./col-->
		</div><!--./row-->
	</div><!--./box-body-->

</div>        
</div><!--./box-->

<script>
    
$(function () {
    $(document).ready(function() {
        $("#study_centre_id").change(function () {
            if ($(this).val() == 1) { // It doesn't work over here.
                $("#showProgramImport").show();
                $("#showProgram").hide();
            } else {
                $("#showProgramImport").hide();
                $("#showProgram").show();
            }
        });
    });
});    

</script>

下拉列表代码

        <div class="col-xs-12 col-sm-6 col-lg-6">
        <?= $form->field($model, 'state_office_id')->widget(Select2::classname(), [
            'data' => ArrayHelper::map(\common\models\StateOffice::find()->where(['is_status' => 0])->all(),'id','state_name'),
            'language' => 'en',
            'options' => ['placeholder' => '--- Select State Office ---', 
                'onchange'=>'
                    $.get( "'.Url::toRoute('dependent/getstudycentre').'", { id: $(this).val() } )
                        .done(function( data ) {
                            $( "#'.Html::getInputId($model, 'study_centre_id').'" ).html( data );
                        }
                    );' 
            ],
         //   'disabled'=>'true',
            'pluginOptions' => [
                'allowClear' => true
            ],
        ]); ?>                            

    </div>
    <div class="col-xs-12 col-sm-6 col-lg-6">
        <?= $form->field($model, 'study_centre_id')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(\common\models\StudyCentre::findAll(['state_office_id' => $model->state_office_id]),'id','study_centre_name'),
            'language' => 'en',
            'options' => ['placeholder' => '--- Select Study Centre ---'],
            'pluginOptions' => [
                'allowClear' => true
            ],
        ]); ?>             
    </div>  

1 个答案:

答案 0 :(得分:1)

我将id添加到下拉div中,如图所示

    <div id="study_centre_id" class="col-xs-12 col-sm-6 col-lg-6">
    <?= $form->field($model, 'study_centre_id')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(\common\models\StudyCentre::findAll(['state_office_id' => $model->state_office_id]),'id','study_centre_name'),
        'language' => 'en',
        'options' => ['placeholder' => '--- Select Study Centre ---'],
        'pluginOptions' => [
            'allowClear' => true
        ],
    ]); ?>             
</div>  

然后,将脚本更改为

<script>
$(document).ready(function() {
    $("#study_centre_id").change(function () {
            if ($(this).find(':selected').val() != 0) {
            $("#showProgramImport").show();
            $("#showProgram").hide();
        } else {
            $("#showProgramImport").hide();
            $("#showProgram").show();
        }
    });
});