Yii2验证码刷新按钮


Yii2 captcha refresh button

我正在使用yii2高级框架的默认captcha实现。我有一个问题:我想添加一个刷新按钮到我的验证码,但我不知道我怎么能做到这一点

添加captcha操作到您的控制器,添加captcha到您的表单,添加字段和验证规则的captcha到您的模型。点击这里阅读更多内容。

并在视图中添加按钮来更新captcha图像,像这样:

<?php echo $form->field($model, 'captcha')->widget(Captcha::className(), [
    'imageOptions' => [
        'id' => 'my-captcha-image'
    ]
]); ?>
<?php echo Html::button('Refresh captcha', ['id' => 'refresh-captcha']);?>
<?php $this->registerJs("
    $('#refresh-captcha').on('click', function(e){
        e.preventDefault();
        $('#my-captcha-image').yiiCaptcha('refresh');
    })
"); ?>