afterAjaxUpdate 回调函数 CListView 显示未定义 [英] afterAjaxUpdate callbackfunction CListView shows undefined

查看:24
本文介绍了afterAjaxUpdate 回调函数 CListView 显示未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过文件名的 afterAjaxUpdate 参数调用在另一个 js 文件中定义的函数,但我在控制台中收到错误,该函数未定义

i am trying to call a function which is defined in another js file by afterAjaxUpdate paramter of the file name but i got error in console that function is not defined

<?php 
$dataProvider=new CActiveDataProvider('profiles',array('pagination'=>array('pageSize'=>3))); ?>
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_profilesview',
'template'=>'{sorter}<br />{pager}{items}{pager}',
    'enableSorting' => true,
    'sortableAttributes'=>array(
     'name'=>'By firstName',
     'location'=>'By city',
     'age'=>'By age',
     'likes'=>'By likes'
     ),
     'afterAjaxUpdate'=>'readcookie()',
));
 ?>

我的js函数是

$(document).ready(function(){
   function readcookie()
   {
       alert("hi");
   }
});

我可以在我的源代码中看到文件中定义的函数包含在yii默认包含的所有js文件之后当我在我的布局中注册我的脚本时,它不会找到 $ 因为当我包含 jquery 时不包含 jquery 它被包含两次导致触发我的事件我也尝试通过设置

i can see in my source that function defined in the file is included after all the js files that are included by yii default when i register my script in my layout it dosent find $ because jquery not included when i include jquery it gets included twice which leads to trigger my event i also tried to by setting

renderPartial('Mybelowview',null,false,true)

renderPartial('Mybelowview',null,false,true)

这再次导致我的 js 文件多次包含并且我的事件被多次触发.

which leads again my js file to include multiple times and my event gots trigger multiple time.

这很令人困惑,请帮助摆脱它谢谢大家这么大方

This is very confusing please help to get out of it Thanks all for being so generous

推荐答案

问题在于 $(document).ready(); 内部的函数超出了它的范围,这就是为什么你得到未定义.所以你可以只拥有:

The problem is that functions inside $(document).ready(); are out of scope outside of it, and that's why you get undefined. So you can either just have:

// $(document).ready(function(){
   function readcookie()
   {
       alert("hi");
   }
// });
// omit document.ready to make function available in the global scope

或者在window对象上定义函数使其成为全局:

or define the function on the window object to make it global:

$(document).ready(function(){
   window.readcookie=function ()
   {
       alert("hi");
   };
});

最后定义属性 'afterAjaxUpdate' 为:

'afterAjaxUpdate'=>'readcookie'
// if it is readcookie() it becomes a function call instead

这篇关于afterAjaxUpdate 回调函数 CListView 显示未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆