今天在用PHP做验证码类的时候,发现以前保存的“JavaScript点击刷新验证码”代码有Bug了,针对火狐浏览器点击验证码不刷新,或者说火狐浏览器点击验证码只刷新一次。
最后总算解决了,说得简单一点,是我以前的“JavaScript点击刷新验证码代码”有问题,不能兼容所有的浏览器,现在已经得到了改正。
以下两种方法都可以实现JS点击刷新验证码功能,只需要一行代码即可解决!亲测可以兼容所有的浏览器。
第一种方法:
<div class="form-group">
<label for="pwd"><strong style="color: red;background-color: white;">*</strong> 验证码:</label>
<input required="required" type="text" name="code" class="form-control" id="pwd" maxlength="4" >
<img src="/admin/code.php" onclick="this.src=this.src+'?p='+Math.random()" />
</div>
第二种方法:
<div class="form-group">
<label for="pwd"><strong style="color: red;background-color: white;">*</strong> 验证码:</label>
<input required="required" type="text" name="code" class="form-control" id="pwd" maxlength="4" >
<img src="/admin/code.php" onclick="this.src='/admin/code.php?p='+Math.random()" />
</div>
给大家看下我的效果图:
总结:
虽然说只有一行的“JavaScript点击刷新验证码”代码,但是,我还是推荐大家用第一种方法,比较省事。