Javascript Not Firing When It Should In Form

hi, I have a form with attributes I set to disable if a checkbox is checked.

My form is very simple


<div class="row">

		<?php echo $form->labelEx($model,'enabled'); ?>

		<?php echo  $form->checkBox($model,'enabled');?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'A'); ?>

		<?php echo  $form->textField($model,'A',array('rows'=>1, 'cols'=>50));?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'B'); ?>

		<?php echo  $form->passwordField($model,'B',array('rows'=>1, 'cols'=>50));?>

	</div>

and just below it I wrote my script like :


<script type="text/javascript">

$(document).ready(function($) {

  if(!$("#myModel_enabled").attr('checked')) {

    $("#myModel_A").attr("disabled",true);

    $("#myModel_B").attr("disabled",true);

  }

  $("#myModel_enabled").change(function(){

    if(!$(this).attr('checked')) {

      $("#myModel_A").attr("disabled",true);

      $("#myModel_B").attr("disabled",true);

    } else if($(this).attr('checked')) {

      $("#myModel_A").attr("disabled",false);

      $("#myModel_B").attr("disabled",false);

    }

  });

});

</script>

I have well defined the css parameter for input[disabled] but nothing happens though…

Can you help ?

Are you sure the script is loaded in your page?

I use this to test for checked.

if ($(’#yourElement’).is(’:checked’)){

}

HTH,

Glenn

It appears in firebug. However nothing is fired… I tried with an alert() or a confirm() and nothing happened.

What is the $ in this line?

$(document).ready(function($) {

Dear Friend

Kindly check the following




<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'myModel-form',

	'enableAjaxValidation'=>false,

)); ?>

<div class="row">

		<?php echo $form->labelEx($model,'enabled'); ?>

		<?php echo  $form->checkBox($model,'enabled');?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'A'); ?>

		<?php echo  $form->textField($model,'A',array('rows'=>1, 'cols'=>50,'disabled'=>true));?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'B'); ?>

		<?php echo  $form->passwordField($model,'B',array('rows'=>1, 'cols'=>50,'disabled'=>true));?>

	</div>

<div class="row buttons">

		<?php echo CHtml::submitButton('Save'); ?>

	</div>


<?php $this->endWidget(); ?>

<?php

Yii::app()->clientScript->registerScript('test','

$("#MyModel_enabled").on("change",function()

   {

	   if($("#myModel-form").find("input:checked").length==0)

	   {

		    $("#MyModel_A,#MyModel_B").attr("disabled",true);	

	   }

		

	  if($("#myModel-form").find("input:checked").length>0)

	   {

		$("#MyModel_A,#MyModel_B").attr("disabled",false);

	   }

	});

');



On page load form elements for attributes A and B are disabled.

After that we can enable or disable by clicking on checbox.

Regards.

Hi, thanks for answering. It doesn’t work.

There is something weird I would like to talk. I had bootstrap extension before and this script works perfectly. However like I am not using it anymore I decided to remove it and remove what concerned it in config/main. And now script doesn’t work anymore. If I put back bootstrap extension and the necessary things in config/main it works again… I don’t see where could be the changes between both states which could influent on firing javascript.

Since you mention that nothing is firing, I would take the simple approach, build the script one block at a time. Test it and add more until you find the problem.

Seems your script file is not loading. either try to load your script.js/somefile.js file by adding your script in your script.js/somefile.js or you need to register the script or script file something like this:

Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl.’/assets/js/jquery.pngFix.js’);//for registering your file

Yii::app()->clientScript->registerScript(‘helloscript’,

    '


    &#036;(&quot;#newCustomer&quot;).click(function () {


            &#036;(&quot;#signup-div&quot;).show();


                    &#036;(&quot;#login-div&quot;).hide();


            });


   





    //&quot;alert('jjjjjjjj');&quot;


    ,CClientScript::POS_READY);//for registering your script in file itself

hope it helps

Thanks all reply,

When I edit my script adding an alert outside the ready(function){…} it is fired (not when I put it inside the function), so I tried do delete this function but even if the alert() is fired the remaining of the script seems to not work… I try to put $("#MyModel_enabled") in the alert but it is not fired then…

I don’t understand why with bootstrap it worked, and not without it. Where is the difference

Does anybody have an Idea of why my scripts doesn’t work without bootstrap activated… I don’t see the link seriously

Because JQuery is generally loaded with bootstrap, creating the $-object?

creating the $-object?

??

Read a little on JQuery; or try just including JQuery in your code and you might find out that it already works.

Ok… it works using :

Yii::app()->clientScript->registerScript(‘script’,"my script … ",CClientScript::POS_READY);

I’m sure I tried it before with developer! answer but I probably got wrong on something when I wrote it.

thanks all