Yii,阿贾克斯,按钮.如何防止多个 JS onclick 绑定 [英] Yii, ajax, Button. How to prevent multiple JS onclick bindings

查看:27
本文介绍了Yii,阿贾克斯,按钮.如何防止多个 JS onclick 绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(首先英语不是我的母语,如果我可能弄错了,我很抱歉).我已经创建了 Yii Web 应用程序,其中是在通过 ajax 请求单击按钮后出现的主页上的输入表单.表单上有一个取消"按钮,可以使带有表单的 div 不可见.如果我单击显示表单"和取消"N 次,然后提交带有数据的表单,请求将重复 N 次.很明显,浏览器在每次出现表单时都会将 onclick 事件绑定到提交按钮上.任何人都可以解释如何防止它?谢谢!

(First of all English is not my native language, I'm sorry if I'll probably be mistaken). I've created Yii Web app where is input form on the 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 a 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.

这基本上是因为您可能使用render()"或renderPartial()返回ajax结果.这每次都会添加 javascript 代码来激活所有 ajax 按钮.如果它们已经处于活动状态,它们现在将被触发两次.所以解决方案是使用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.

这篇关于Yii,阿贾克斯,按钮.如何防止多个 JS onclick 绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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