第一次验证码不工作



我正在尝试创建注册表单,我使用验证码。我已经启用了ajax验证。但是有一个问题,验证码第一次不能工作。当我生成新的captcha码时,它就可以工作了。我的代码是:

在模型中

public $verifyCode;
public function rules()
{
return array(
    array('verifyCode','captcha','allowEmpty'=>!CCaptcha::checkRequirements(), 'on'=>'registration'),
    array('id, first_name,last_name, email, username, password, password_repeat, user_type,  keystring, status, logo_url, last_login_time, create_time, update_time, validation', 'safe', 'on'=>'search'),
    );
}
在控制器:

public function actions()
{
    return array(
        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'backColor'=>0xFFFFFF,
            'testLimit'=>3,
        ),
    );
}
在视图:

<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
    <?php echo $form->labelEx($model,'verifyCode'); ?>
    <?php $this->widget('CCaptcha', array('captchaAction'=>'user/captcha')); ?>
</div>
<div class="formfieldarea">
    <div class="form-text"></div>
    <div class="form-field">
        <?php echo $form->textField($model,'verifyCode'); ?>
        <div class="hint">Please enter the letters as shown.
            <br/>Letters are not case-sensitive.</div>
        <?php echo $form->error($model,'verifyCode'); ?>
    </div>
</div>
<?php endif; ?>

我在谷歌搜索了很多找到解决方案。但没有找到。

作为一个建议,按照你的观点做:

<script type="text/javascript">
    function captchaInit(){
            $("#yw0_button").click();
    }
</script>

然后,在你的身体onload调用该函数。

<body onload="captchaInit()"> 

导致在body load中重新加载Captcha。在类似的情况下,它可以在每次刷新页面时刷新验证码。

最新更新