最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Refresh Simple Captcha - Stack Overflow

programmeradmin3浏览0评论

I am using simple captcha in my JSP. Everything is OK. I want to provide a refresh button alongside of captcha to allow user to change the captcha. Captcha changes only on refreshing the plete page but I don't want to reload whole page for it.

I need your suggestions on how I can implement this like using AJAX or JQuery to reload only the captcha, not whole page.

I am using simple captcha in my JSP. Everything is OK. I want to provide a refresh button alongside of captcha to allow user to change the captcha. Captcha changes only on refreshing the plete page but I don't want to reload whole page for it.

I need your suggestions on how I can implement this like using AJAX or JQuery to reload only the captcha, not whole page.

Share Improve this question edited Apr 14, 2012 at 7:48 Andrew Thompson 169k41 gold badges222 silver badges436 bronze badges asked Apr 14, 2012 at 7:37 SandySandy 14.1k22 gold badges79 silver badges111 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3
<script type="text/javascript">
    function reloadCaptcha(){
        var d = new Date();
        $("#captcha_image").attr("src", "/captcha_generator.jsp?"+d.getTime());
    }
</script>
   ...
<img id="captcha_image" src="/captcha_generator.jsp" alt="captcha image" width="200" height="50"/>
<img src="reload.jpg" onclick="reloadCaptcha()" alt="reload"width="40" height="40"/>

I don't remember how SimpleCaptcha works, but usually, you should simply change the 'src' attribute of you captcha <img>. Something like this on the onClick of your refresh button:

var img = document.getElementById('captcha_id');// captcha_id is the id attribute of your caprtcha img
img.src = 'Some new captcha url';
<script type="text/javascript">
    function reloadCaptcha(){
        var d = new Date();
        $("#captcha_image").attr("src", "captcha.php?"+d.getTime());
    } </script>

<img id="captcha_image" src="captcha.php" alt="captcha image" width="90" height="33"/> <img src="images/refresh.png" alt="reload" width="22" height="22" border="0" onclick="reloadCaptcha()" />
发布评论

评论列表(0)

  1. 暂无评论