Yii Framework Forum: Ajax Checkbox toggles - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ajax Checkbox toggles Problems getting checkboxes to toggle when AJAX is applied Rate Topic: -----

#1 User is offline   Ocean Wind 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 14-September 10
  • Location:Denver, Colorado, USA

Posted 04 May 2011 - 08:54 PM

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!
0

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,522
  • Joined: 12-October 09
  • Location:Croatia

Posted 05 May 2011 - 02:16 AM

View PostOcean Wind, on 04 May 2011 - 08:54 PM, said:

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


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



Have you tryed

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


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

Find more about me.... btw. Do you know your WAN IP?
0

#3 User is offline   Ocean Wind 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 14-September 10
  • Location:Denver, Colorado, USA

Posted 05 May 2011 - 12:52 PM

View Postmdomba, on 05 May 2011 - 02:16 AM, said:

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!
0

#4 User is offline   Ocean Wind 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 65
  • Joined: 14-September 10
  • Location:Denver, Colorado, USA

Posted 26 October 2011 - 06:00 PM

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.yiiframew...-toggle-script/
0

#5 User is offline   shafi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 03-August 12
  • Location:cochin keral india

Posted 16 October 2012 - 04:20 AM

<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
0

#6 User is offline   shafi 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 03-August 12
  • Location:cochin keral india

Posted 16 October 2012 - 04:21 AM

[quote name='shafi' timestamp='1350379202' post='176483']
<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......
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users