简体   繁体   English

Yii2-显示基于dropDownList的表单字段

[英]Yii2 - Show form fields based on dropDownList

I have the following dropDownList 我有以下dropDownList

<?= $form->field($model, 'moradaalternativa')
    ->dropDownList(
        [
        'Não' => 'Não',
        'Sim' => 'Sim'],
        ['prompt'=>'Faça a sua escolha'],
    );
?>

What i'm trying to do is: If the value is = Sim, then some other form fields that are hidden by a css class show up below the dropDownList, and if the value is = Não, then the form fields hide from the page again. 我想做的是:如果值是= Sim,则由CSS类隐藏的其他一些表单字段会显示在dropDownList下方,如果值是=Não,则表单字段将从页面中隐藏再次。

I know that there is a 'onchange' property like javascript but i don't know how to apply it to this effect or even if i need to use it. 我知道有一个像javascript这样的'onchange'属性,但是我不知道如何将其应用于这种效果,甚至我都不需要使用它。

Any ideas? 有任何想法吗?

It's pretty simple, all you need is: 这很简单,您需要做的是:

$(document).ready(function () {
    $(document.body).on('change', '#your-id', function () {
        var val = $('#your-id').val();
        if(val > 0 ) {
          $('.class').hide();
        } else {
          $('.class').show();
        }
    });
});

And just change the names as needed. 并根据需要更改名称。 For Yii2 you can wrap it, then you can just put the code in a view file but it's better to put it in a JS file: 对于Yii2,您可以将其包装,然后将代码放入视图文件中,但最好将其放入JS文件中:

<?php
$script = <<< JS

code here

JS;
$this->registerJs($script);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM