Refresh Simple Captcha

删除回忆录丶 提交于 2019-12-07 05:42:03

问题


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 complete 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.


回答1:


<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"/>



回答2:


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';



回答3:


<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()" />


来源:https://stackoverflow.com/questions/10151920/refresh-simple-captcha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!