我试图调用另一个js文件中通过文件名的afterAjaxUpdate参数定义的函数,但我在控制台中收到错误,该函数未定义
<?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 它会包含两次,这会导致触发我的事件 我也尝试通过设置
renderPartial('Mybelowview',null,false,true)
这再次导致我的 js 文件包含多次,并且我的事件多次触发。
这非常令人困惑,请帮助摆脱它 感谢大家如此慷慨
最佳答案
问题是 $(document).ready();
内部的函数超出了它外部的范围,这就是为什么你会得到未定义。所以你可以只拥有:
// $(document).ready(function(){
function readcookie()
{
alert("hi");
}
// });
// omit document.ready to make function available in the global scope
或者在窗口对象上定义函数以使其全局:
$(document).ready(function(){
window.readcookie=function ()
{
alert("hi");
};
});
最后将属性'afterAjaxUpdate'
定义为:
'afterAjaxUpdate'=>'readcookie'
// if it is readcookie() it becomes a function call instead
关于jquery - afterAjaxUpdate 回调函数 CListView 显示未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10960829/