doubao6936 2015-03-04 21:42
浏览 40
已采纳

Yii:javascript的参数化调用 - 引号问题

I am using Yii Version 1.1.15.

In a View I use ajaxlink and it works like a charme:

<?=
  CHtml::ajaxLink(
     '[Help]',
     $this->createUrl('genearal/help'), 
     array(
        'onclick' => '$("#help").dialog("open"); return false;',
        'update' => '#help'), 
     array('id' => 'showHelp')
  );
?>
<div id="help"></div>

Since I have a lot of ajaxlink-calls, I want to write a function in the components-folder which I can call with: MyComponents::help("Help", "general/help", "help", "showHelp");

class MyComponents {
    public static function help($headline, $controller_and_action, $divname, $idname) {
              CHtml::ajaxLink(
                 '[$headline]',
                 $this->createUrl('$controller_and_action'), 
                 array(
                   'onclick' => '$("$divname").dialog("open"); return false;',
                   'update' => '$divname'), 
                 array('id' => '$idname')
              );
    }
}

Problem: If I call this in the browser, I only get an empty page.

Do I have to extend MyComponents with a parentclass? Which? (I don't think so: I tested echo CHtml::encode("test") without problems.

Maybe something wrong with my quotes?

I was googeling for hours and gave up. Hope it's not impossible.

  • 写回答

1条回答 默认 最新

  • douboshan1466 2015-03-04 23:29
    关注

    I solved the problem. It was very simple. I didn't get an erromessage and therefore thought that something was going wrong with javascript.

    In my controller I am not in an object-context - so $this->createUrl(...) is not possible. The solution:

    Yii::app()->createUrl(...)
    

    ... and it works like a charme ;-)

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题