Ajax Checkbox toggles

Hi all,

I’m applying some AJAX to a checkbox to get it to auto update a model when checked. But no matter what I do, I can’t get the checkbox to toggle. The model updates fine. I’ve used FireBug to verify that the return values are returning as expected. I’m not getting any errors.

I’ve tried using every conceivable trick I’ve been able to find to change the checked value of the checkbox, the most straightforward of which is:

ON


$('.myCheckbox').attr('checked','checked')

OFF


$('.myCheckbox').removeAttr('checked')

But the checkboxes won’t toggle as long as AJAX is applied – even if I don’t try to apply my returned values.

Here’s my code, where I’ve tried to attach the checked status to the return values:


	

foreach($recommenderTypes as $type) {

	$checked = isset($validRecommenders[$type->id]);

?>

<?php echo CHtml::checkBox('recommenderType_'.$type->id, $checked, array(

		'ajax' => array(

			'type'=>'POST', //request type

			'url'=>  $this->createUrl('admin/programType/ajaxCheckboxToggle'),

			'dataType'=>'text',

			'data' => array('ap_programType_id' => $model->id,'ap_recommenderType_id' => $type->id),

                        'success' => 'js:function(vals){

				if (vals == 0) { $(this).removeAttr("checked"); }

				else if (vals == 1 ) { $(this).attr("checked","checked"); }

				else alert("Server disconnect. Please reload the page and try again.");

			}',

			'error'=>'function (xhr, ajaxOptions, thrownError){

				alert(xhr.statusText);

				alert(thrownError);}',

		),

		'prompt' => Yii::t('app','- select -'),

	)); ?> <?=$type->name?><br />

<?php } ?>

Any ideas? Is this a JUI bug?

Thanks!

Have you tryed

ON


$('.myCheckbox').attr('checked',true)

OFF


$('.myCheckbox').attr('checked',false)

That worked. And it turns out I was missing the ‘.’ before the fieldname (although I had it in this example).

Thanks!

I’ve started a new thread based about this AJAX checkbox toggle (which includes a solution to the problem) — and raises another issue I’m having with mysterious 302 Found errors being generated:

http://www.yiiframework.com/forum/index.php?/topic/25189-ajax-302-errors-in-checkbox-toggle-script/




<div class="myCheckbox">

<?php 

$u='';

 echo CHtml::checkbox('my_checkbox',$u , array(

                                'id'=>'check',

							    

							    

							    'ajax' => array(

                                'type'=>'POST',

                                'url'=>'index.php?r=realestate/default/checkboxUpdate',

                                'data'=>'shafi',

                                'success'=>"function(data){

                                	$(this).attr('checked',true);

                                		

                                	}",

                               

)

)); ?>






not working…what happened to this…please help me Ocean Wind :-[ thanks in advance

[quote=“shafi, post:5, topic:37908”]




<div class="myCheckbox">

<?php 

$u='';

 echo CHtml::checkbox('my_checkbox',$u , array(

                                'id'=>'check',

							    

							    

							    'ajax' => array(

                                'type'=>'POST',

                                'url'=>'index.php?r=realestate/default/checkboxUpdate',

                                'data'=>'shafi',

                                'success'=>"function(data){

                                	$(this).attr('checked',true);

                                		

                                	}",

                               

)

)); ?>






help me out…i m totally stucked on this…