Yii - jQuery 在 renderPartial 之后不工作

发布于 2025-01-01 16:38:49 字数 1391 浏览 1 评论 0原文

我知道这个问题已经被问过很多次了,无论是在这里还是在 Yii 网站上,但即使在浏览了每个解决方案之后我也没有得到解决方案。我确信我正在做一些根本错误的事情。因此,如果您仍在阅读本文 - 这里什么也没有,

我有一个页面正在加载 CheckBoxList 小部件。它有一个change() jQuery 函数调用,可以刷新CGridView,效果非常好。我还有一个与 checkBoxList 中具有单击事件的每个项目相关的跨度。

此单击事件依次调用“部分呈现”视图的控制器 -

Yii::app()->clientscript->scriptMap['jquery.js'] = false;


$this->widget('ext.xxx.EModelWidget', array(
'name' => $widget_name,
'manufacturer_id' => $manufacturer_id,
    )
);

并且相应的小部件代码在从数据库获取 $data 后打印 CheckBoxList。

echo CHtml::checkBoxList($this->name, array(), $data, array(
        'class' => 'modelFilter',
        'separator' => '',
        'template' => '<div>{input} {label}</div>',
        'id' => 'dummyID',
    ));

如您所见,我已将每个将呈现为复选框的元素的类名称设置为 modelFilter

现在,我在主视图文件中有一个脚本,用于在复选框被选中时显示警告框单击 modelFilter 类的。

Yii::app()->clientScript->registerScript('model-selected', "
$('input.modelFilter').change(function(){
  alert('Hellpo!');
  });
");

但不幸的是,这种情况永远不会发生。

我在所有论坛中读过的两个常见解决方案是

a) Yii::app()->clientscript->scriptMap['jquery.js'] = false; 您可以看到我已经正式遵循了。

b) renderPartial 的 $result 和 $processOutput 参数分别为 false 和 true。,这也是我遵循的。

所以我的问题是我应该怎么做才能在 Yii 中通过 Ajax 加载的内容上获得更改功能!

I know this question has been asked numerous number of times, both here and in the Yii site, but I am not getting the solution even after going through each solution. I am sure I am doing something fundamentally wrong. So if you are still reading this - here goes nothing,

I have a page that has a CheckBoxList widget being loaded. It has a change() jQuery function call which refreshes a CGridView which works perfectly well. I also have a span related to each of the items in the checkBoxList which have a click event.

This click event, in turn calls a controller which "partially renders" a view -

Yii::app()->clientscript->scriptMap['jquery.js'] = false;


$this->widget('ext.xxx.EModelWidget', array(
'name' => $widget_name,
'manufacturer_id' => $manufacturer_id,
    )
);

And the corresponding widget code prints a CheckBoxList after fetching $data from the db.

echo CHtml::checkBoxList($this->name, array(), $data, array(
        'class' => 'modelFilter',
        'separator' => '',
        'template' => '<div>{input} {label}</div>',
        'id' => 'dummyID',
    ));

As you can see, I have put the class name for each element that will be rendered as a checkbox to be modelFilter

Now I have a script in the main view file to just display an alert box when the checkBox of class modelFilter is clicked.

Yii::app()->clientScript->registerScript('model-selected', "
$('input.modelFilter').change(function(){
  alert('Hellpo!');
  });
");

But unfortunately, this never happens.

The two common solutions which I have read in all the forums is

a) Yii::app()->clientscript->scriptMap['jquery.js'] = false; which you can see that I have duly followed.

b) $result and $processOutput parameters of the renderPartial to be false and true respectively., which is also something that I have followed.

So my question is what should I do to just get the change function on a content that is loaded via Ajax working, in Yii!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩套路吗 2025-01-08 16:38:49

如果我理解正确,您将在加载复选框列表之前调用 $('input.modelFilter').change() 。像这样使用事件处理程序将不会绑定到调用后插入的任何元素。

您必须使用委托事件而不是直接绑定事件。看一下:http://api.jquery.com/on/(搜索“直接和委托活动”)

$("body_or_input_container").on("change", "input.modelFilter", function(e){
    //callback
});

If I understand correctly, you are calling $('input.modelFilter').change() before you load the list of checkboxes. Used like that the event handler will not be bound to any elements inserted after the call.

You have to use delegated events instead of directly bound events. Take a look at: http://api.jquery.com/on/ (search for "Direct and delegated events")

$("body_or_input_container").on("change", "input.modelFilter", function(e){
    //callback
});
多情癖 2025-01-08 16:38:49

我遇到了类似的问题,在执行 Ajax 调用后,该调用返回了部分渲染的 HTML,其中包含 javascript(由 Yii 的 CActiveForm 自动生成),一切都被破坏了,因为不再识别 jQuery 插件。

经过长时间的研究,我发现该部分再次发送 jQuery lib,并且它通过“忘记”之前初始化的所有内容来重新初始化自己(特别是其他插件,在我的例子中是 UI 对话框)。
唯一的修复是告诉 Yii 不要在部分中包含 jQuery.js 本身,而是包含其他 JS 文件。

所以把它放在部分的开头。

Yii::app()->clientscript->scriptMap['jquery.js'] = false;

请注意,您必须配置为在页面加载时加载 jQuery。

I had a similar issue, after I performed an Ajax call which returned a partial rendered HTML with javascript inside (auto generated by Yii's CActiveForm) everything was broken because no jQuery plugin has been recognized anymore.

After a long research I found that the partial is sending the jQuery lib again and it reinitializes itself by "forgetting" everything what has been initialized before (especially other plugins, in my case the UI dialog).
The only fix was to tell Yii to not include the jQuery.js itself in the partial but let include other JS files.

So put this at the beginning of the partial.

Yii::app()->clientscript->scriptMap['jquery.js'] = false;

Note that you must configure to load the jQuery once at the page load.

唯憾梦倾城 2025-01-08 16:38:49

好的,我发现的解决方案(无论是否意外)是将部分渲染的文件的 jQuery 脚本包含在部分渲染的文件本身中。

不过我想知道这种风格是否正确。

OK, on solution that I found, be it accidentally, was to include the jQuery script for the partially rendered file, in the partially rendered file itself.

I want to know if this style is correct, though.

橙味迷妹 2025-01-08 16:38:49

在眼睛 renderPartial 的情况下,它会破坏 javascript 的现有对象,

例如

,我有一个带有欧芹 j 验证的表单,当我们使用 render () 但无法识别欧芹对象时,它可以完美地工作。所以我们需要再次重新初始化该对象,在我的例子中它是

$("#form").parsley();

In case of eye renderPartial it destroys the existing object of javascript

e.g

I have a form with parsley j's validation it works perfectly when we use render () but doesn't recognize the parsley object. so we need to reinitialize the object again in my case it was

$("#form").parsley();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文