Zugluk
(Lacffy Duck)
January 9, 2013, 4:43pm
1
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 ?
maestroQC
(Maestroqc)
January 9, 2013, 4:57pm
2
Are you sure the script is loaded in your page?
I use this to test for checked.
if ($(’#yourElement ’).is(’:checked’)){
}
HTH,
Glenn
Zugluk
(Lacffy Duck)
January 9, 2013, 5:28pm
3
It appears in firebug. However nothing is fired… I tried with an alert() or a confirm() and nothing happened.
maestroQC
(Maestroqc)
January 9, 2013, 5:47pm
4
What is the $ in this line?
$(document).ready(function($ ) {
seenivasan
(Chellamnivas)
January 9, 2013, 6:48pm
5
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.
Zugluk
(Lacffy Duck)
January 10, 2013, 9:08am
6
seenivasan:
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.
maestroQC
(Maestroqc)
January 10, 2013, 2:37pm
7
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’,
'
$("#newCustomer").click(function () {
$("#signup-div").show();
$("#login-div").hide();
});
//"alert('jjjjjjjj');"
,CClientScript::POS_READY);//for registering your script in file itself
hope it helps
Zugluk
(Lacffy Duck)
January 11, 2013, 9:21am
9
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
Zugluk
(Lacffy Duck)
January 15, 2013, 9:53am
10
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?
Read a little on JQuery; or try just including JQuery in your code and you might find out that it already works.
Zugluk
(Lacffy Duck)
January 17, 2013, 10:58am
14
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