What is the best way to hid part of a form based on the content of a drop down list?
If the drop down list would call back to the controller ever time it was edited I suppose I could set some flags and re-render the page differently. Is there any essay way to do this? Is there a better way to hid part of a form based on the content of a drop down list?
Wrap that part into a <fieldset> and use jQuery to toggle its visibility when the dropdown changes. Or if what you want is only at render time, you set its visibility value by css in PHP.
Do you know where I can find an example of this? I am not very familiar with jQuery. I need it to change every time the user changes the drop down list selection, so I think the css thing will not work. Thanks
Using jQuery change event, and toggle() or hide and show() methods, you’ll have something like this in your view (adapt accordingly to your test):
Yii::app()->clientScript->registerScript("someScript", "
$('#someFieldset').hide();
$('#ModelName_someAttribute').change(function() {
// Let's test that the value is not empty ie a value has been selected
$('#someFieldset').toggle($(this).val() != '');
});
");
<?php $form=$this->beginWidget('CActiveForm', array(
...
));
...
<?php echo $form->dropDownList($model,'someAttribute',
$someDataArray,
array('empty' => 'Select something')
); ?>
...
<fieldset id="someFieldset">
...
</fieldset>
...
<?php $this->endWidget(); ?>
I think I am not getting the (’#ModelName_someAttribute’) part correct I have a model that is just called ‘$model’ and the attribute is ‘question_type’, so I was trying (’#model_question_type’) and (’#question_type’), but they do not work. What am I doing wrong? In the end I want to hide when the drop down list value is ‘SHORT-ANSWER’ and show when it is ‘MULTIPLE-CHOICE’. Thanks