Yii2验证码刷新或重新加载图像

Yii2验证码刷新或重新加载图像,yii2,client-side,javascript,php,jquery,Yii2,Client Side,Javascript,Php,Jquery,我正在使用随附的高级应用程序模板的默认验证码实现。我尝试了很多次,试图找到任何api属性,允许我添加刷新图像链接,允许用户生成新的验证码图像,但我不知道如何做到这一点。我根据Jquery尝试了以下解决方案: <!-- This code is placed just before the closing </body> tag at the end of "frontend/views/layouts/main.php" --> <script> $("#

我正在使用随附的高级应用程序模板的默认验证码实现。我尝试了很多次,试图找到任何api属性,允许我添加
刷新
图像链接,允许用户生成新的验证码图像,但我不知道如何做到这一点。我根据Jquery尝试了以下解决方案:

<!-- This code is placed just before the closing </body> tag at the end of
"frontend/views/layouts/main.php" -->
<script>
  $("#fox").click(function(event){
    event.preventDefault();
    $("img[id$='-verifycode-image']").click();
  })
</script>
上述解决方案工作正常,但必须为所有应用程序视图添加不可用的代码,除了
contactAction
视图或任何其他使用Captcha的视图!因为我无法在包含Jquery库之前插入代码。i、 e,因此我将其插入布局的末尾


这里的问题是:有没有直接从yii2api获得的解决方案?或者有什么更好的建议让jquery代码在视图中工作?i、 e将其转换为纯JavaScript

将其移动到外部文件,您应该会没事。
另一种解决方案是创建带有验证码的小部件,创建带有代码的文件,并在调用小部件时注册javascript文件。
或者只是用javascript创建一个文件,并记住在每次显示验证码时注册它


选择第一个解决方案,更简单,你不需要构建任何东西,等等。在没有数千个并发用户的网站上,它会工作得很好。

下面的代码对我很有用-

查看文件上的表单代码以生成验证码图像-

<?= $form->field($model, 'captcha')->widget(\yii\captcha\Captcha::classname(), [
    'template' => '<div class="col-lg-3">{image} <a href="javascript:;" id="fox">Refresh</a></div><div class="col-lg-3">{input}</div>',
])?> 
和js代码刷新layouts/main.php文件中的验证码图像-

<script>
  $("#fox").on('click',function(e){
    //#testimonials-captcha-image is my captcha image id
    $("img[id$='-captcha-image']").trigger('click');
    e.preventDefault();
  })
</script>

$(“#fox”)。在('click',函数(e){
//#认证验证码图像是我的验证码图像id
$(“img[id$='-captcha image'])。触发器('click');
e、 预防默认值();
})

这将解决这个问题,我们可以创建一个中心视图作为验证码的模板,并在任何地方调用它

<?php
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\ContactForm */

?>
<div class="row">
    <div class="col-lg-3">{image} <br /><a id="refresh_captcha" href="javascript:;">Refresh Captcha</a></div>
    <div class="col-lg-6">{input}</div>
</div>
<?php
$this->registerJs('
    $(document).ready(function () {
        $("#refresh_captcha").click(function (event) {
            event.preventDefault();
            $(this).parent().children(\'img\').click();
        })
    });
    ');
?>
我在
contact.php
中跟踪文档到
registerJsFile
,但返回了一个错误:找不到类'jqueryaset'。
<script>
  $("#fox").on('click',function(e){
    //#testimonials-captcha-image is my captcha image id
    $("img[id$='-captcha-image']").trigger('click');
    e.preventDefault();
  })
</script>
<?php
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\ContactForm */

?>
<div class="row">
    <div class="col-lg-3">{image} <br /><a id="refresh_captcha" href="javascript:;">Refresh Captcha</a></div>
    <div class="col-lg-6">{input}</div>
</div>
<?php
$this->registerJs('
    $(document).ready(function () {
        $("#refresh_captcha").click(function (event) {
            event.preventDefault();
            $(this).parent().children(\'img\').click();
        })
    });
    ');
?>
<?= $form->field($model, 'verificationCode')->widget(yii\captcha\Captcha::className(),[
                'template' => $this->render("/_partials/captcha_template"),
            ]); ?>
$(this).parent().children(\'img\').click();