Yii中,阿贾克斯,巴顿。如何prevent多个JS的onclick绑定巴顿、多个、绑定、阿贾克斯

2023-09-10 13:52:34 作者:曾经的回忆美好到让我流泪

(首先,英语不是我的母语,我很抱歉,如果我可能错了)。 我创建Yii的Web应用在哪里出现后按一下按钮,通过Ajax请求主页上输入表单。有使分度表看不见的窗体上的一个取消按钮。如果我点击查看表和取消N次,然后用数据请求被重复N次提交表单。显然,浏览器绑定onclick事件提交按钮,每次的形式出现。任何人都可以解释如何prevent呢? 谢谢!

(First of all english is not my native language, I'm sorry if I'll probably mistaken). I've created Yii Web app where is input form on main page which appears after button click through ajax request. There is a "Cancel" button on the form that makes div with form invisible. If I click "Show form" and "Cancel" N times and then submit form with data the request is repeating N times. Obviously browser binds onclick event to the submit button every time form appears. Can anybody explain how to prevent it? Thank you!

推荐答案

我有完全相同的问题,有关于它的讨论 Yii的论坛。

I've had the exact same problem and there was a discussion about it in the Yii Forum.

这基本上是因为你很可能返回与阿贾克斯的结果的渲染()的,而不是或的的RenderPartial()的。这每一次激活所有的Ajax按钮添加JavaScript的code。如果他们已经激活他们现在将触发两次。因此,解决方案是使用的的RenderPartial()的。无论是使用渲染仅第一次,然后的的RenderPartial()的,或使用的的RenderPartial()的从一开始但要确保的 processOutput 的参数仅设置为true的第一次。

This basically happens because you are probably returning ajax results with "render()" instead or renderPartial(). This adds the javascript code every time to activate all ajax buttons. If they were already active they will now be triggered twice. So the solution is to use renderPartial(). Either use render the first time only and then renderPartial(), or use renderPartial() from the start but make sure the "processOutput" parameter is only set to TRUE the first time.