Hey guys. Im trying to hide 2 dependent dropdownlist and it’s labels and show them only when user selects ‘apoderado’ in the first dropdownlist.
here’s the code, I hope someone can help me 'cause I’m new to jquery and js.
RegisterAsset.php
<?php
namespace app\assets;
use yii\web\AssetBundle;
class RegisterAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
];
public $js = [
'js/esconderDropDownList.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\web\JqueryAsset',
'yii\bootstrap\BootstrapAsset',
];
}
esconderDropDownList.js
$(document).ready(function(){
$('#formregister-role').on('change', function() {
if ($(this).val() == 1){
$("#formregister-id_curso, #formregister-id_alumno").show();
} else {
$("#formregister-id_curso, #formregister-id_alumno").hide();
}
});
});
register.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\assets\RegisterAsset;
RegisterAsset::register($this);
?>
<h3><?= $msg ?></h3>
<h1>Registrar Usuario</h1>
<?php $form = ActiveForm::begin([
'method' => 'post',
'id' => 'formulario',
'enableClientValidation' => false,
'enableAjaxValidation' => true,
]);
?>
<?php $tipo_usuario = [1 => 'Apoderado', 2 => 'Profesor']; ?>
<div class="form-group">
<?= $form->field($model, "role")->dropDownList($tipo_usuario,
[
'prompt' => 'Seleccione tipo de usuario',
]);
?>
</div>
<div class="form-group">
<?= $form->field($model, 'id_curso')->dropDownList(
ArrayHelper::map(app\models\Curso::find()->all(),'id', 'id'),
[
'prompt' => 'Seleccione un curso',
'onchange' => '$.post("index.php?r=site/listar&id=' . '"+$(this).val(), function(data){
$("select#formregister-id_alumno").html(data);
});',
]); ?>
</div>
<div class="form-group">
<?= $form->field($model, 'id_alumno')->dropDownList(
ArrayHelper::map(app\models\Alumno::find()->all(),'id', 'nombre'),
[
'prompt' => 'Seleccione un alumno',
]); ?>
</div>
<div class="form-group">
<?= $form->field($model, "username")->input("text") ?>
</div>
<div class="form-group">
<?= $form->field($model, "email")->input("email") ?>
</div>
<div class="form-group">
<?= $form->field($model, "password")->input("password") ?>
</div>
<div class="form-group">
<?= $form->field($model, "password_repeat")->input("password") ?>
</div>
<?= Html::submitButton("Registrar", ["class" => "btn btn-primary"]) ?>
<?php $form->end() ?>