Disable/enable Some Input Fields In Form Dinamically

Hello everybody,

Here is my will, in my form, I have many fields which depends on others, and I would like to be able to disabled or enabled these fields depending on what the user chose on the related fields.

Example : I have a dropdownlist field with 2 choices possible "PC" or "MAC". If the user chooses "PC" so the field Operating system stays enabled and the user can write anything he wants. If the user chooses "MAC" the field Operating system becomes disabled and the users cannot write in it anymore.

I guess I have to use javascript to reach that, but I don’t really know how to do it and maybe yii integrates already some functions or widgets to do it.

thanks.

I am not sure if yii has something of that sort, but its very straight forward create something similar here is one i hacked it in 5 minutes


<!-- form  -->

<form action="" method="post">

	<select name="os" id="selec-os">

		<option value="pc">PC</option>

		<option value="mac">MAC</option>

	</select>

	<input type="text" name="os" id="os-field" />

	<input name="submit" type="submit" value="Submit" />

</form>


<!-- javascript  -->

<?php Yii::app()->clientScript->registerCoreScript("jquery"); ?>

<script type="text/javascript">

$(document).ready(function() {

	$("#selec-os").change(function(){

		if($(this).val() === "mac") {

			$("#os-field").attr("disabled", "disabled");

		} else {

			$("#os-field").removeAttr('disabled');

		}

	});

});

</script>

Thanks alirz, I try it =D