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 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题