只是两个快速的片段,但我想你会得到这个想法.根据需要调整元素名称和选择器.
原文链接:https://www.f2er.com/php/139818.htmlpublic function refreshAction() { $form = new Form_Contact(); $captcha = $form->getElement('captcha')->getCaptcha(); $data = array(); $data['id'] = $captcha->generate(); $data['src'] = $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix(); $this->_helper->json($data); }
在你的视图脚本(我使用mootools为ajax请求)
document.addEvent('domready',function() { $$('#contactForm img').addEvent('click',function() { var jsonRequest = new Request.JSON({ url: "<?= $this->url(array('controller' => 'contact','action' => 'refresh'),'default',false) ?>",onSuccess: function(captcha) { $('captcha-id').set('value',captcha.id); $$('#contactForm img').set('src',captcha.src); } }).get(); }); });
编辑:添加pahan的jquery
$(document).ready(function() { $('#refreshcaptcha').click(function() { $.ajax({ url: '/contact/refresh',dataType:'json',success: function(data) { $('#contactForm img').attr('src',data.src); $('#captcha-id').attr('value',data.id); } }); }); });