douya7309 2012-05-04 14:53
浏览 77
已采纳

Chtml CheckBox单击复选框但仍标记为未选中且未更改。通过ajax提交

    <?php echo CHtml::checkBox('markComplete', FALSE , 
                    array(
                    'class' => 'markComplete',
                    'id'=> 'markComplete'.$events['id'],                        
                    'ajax' => array(
                                    'url'=>$this->createUrl('/events/events/MarkComplete'),
                                    'data'=>'event_status_on='.$events['id'],
                                    'type'=>'POST',
                                    'beforeSend' => 'js:function(){   

                                        var idSelector = $("#markComplete'.$events['id'].'");   
                                        var check = idSelector.is(":checked");  
                                        var child = idSelector.parent("td");
                                        var parent = $(child).parent("TR");
                                        $(parent).css("opacity","0.5");


                                    }',
                                    'success'=>'js:function(resp) {                                                                         
                                        $("#right").prepend(resp);

                                        return true;                                        
                                    }'
                                    ),
                                )

                            );

    ?>

Chtml CheckBox click on checkbox but still marked as unchecked not changing.. if i change it to TRUE then it not change into FALSE... Submit it thrugh AJAX... Getting response perfectly.

  • 写回答

1条回答 默认 最新

  • doumiao0498 2012-05-04 15:54
    关注

    There is no error in your code, but the onclick handler that is finally generated for the checkBox returns false by default. Check your generated html, and you'll see this:

    jQuery('body').undelegate('#markComplete_someId','click').delegate('#markComplete_someId','click',function(){jQuery.ajax({'url':'/something','data':'event_status_on=someId','type':'POST','beforeSend':function(){
         var idSelector = $("#markComplete");
         var check = idSelector.is(":checked");
         var child = idSelector.parent("td");
         var parent = $(child).parent("TR");
         $(parent).css("opacity","0.5");
    },
    'success':function(resp) {
          $("#right").prepend(resp);
          return true;
    },'cache':false});
     return false; // this line changes everything!!
    });
    

    That return false; in the end prevents the default onclick event behavior of any html element. This is mentioned in the documentation for the clientChange method. You'll see that it allows us to specify certain special attributes for htmlOptions, such as 'ajax' that you have already used, and also 'return', that specifies what the onclick handler should return, which is false by default, we can change this to return true, by adding the 'return' property like this:

     <?php echo CHtml::checkBox('markComplete', FALSE , 
                    array(
                       'class' => 'markComplete',
                       'id'=> 'markComplete'.$events['id'],                        
                       'ajax' => array( /* add your code */ ),
                       'return' => true // add return like this
                    )
       );
    ?>
    

    That will make it work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?