使用ajax更改验证代码

change verfication code using ajax

本文关键字:验证 代码 ajax 使用      更新时间:2023-09-26

我想实现以下代码:

verify.html:

<input type="text" id="vericode" style="position:relative;top:-3px;width:70px;height:20px;border:1px solid #928b8b" name="verif_box">
<IMG style="position:relative;top:5px;margin-left:5px;" SRC="image.php">
    <a href="#" id="change">change to another one</a>
<script>
    $(document).ready(function(){ 
        $("#change").click(function(e){
            e.preventDefaut();
            //not sure how to load the image.php again?
        }
   });
</script>

我想在点击"更改为另一个"时更改验证码,不知道如何重新加载image.php,每次当verify.html刷新时,验证码将更改为一个新的,但我想更改它而不需要每次刷新页面,任何人都可以帮助我解决这个问题,您的帮助将非常感谢。

您可以通过修改img的src属性并添加时间戳来重新加载图像,这样链接将是唯一的,并且不会使用缓存版本。

$("#change").click(function(e){
  e.preventDefaut();
  $("img").attr('src', 'image.php?ts=' + new Date().getTime());
}

请将id属性添加到您想要重新加载的图像中,以便您可以使用$("#imgID")

我会以不同的方式处理这个问题,并通过ajax调用image.php文件,用该调用返回的图像替换div的内容。

如果您对这种方法感到满意,那么另一个堆栈溢出问题可以帮助您。

HTML中也有一些错误。

<input type="text" id="vericode" style="position:relative;top:-3px;width:70px;height:20px;border:1px solid #928b8b" name="verif_box" />
<IMG style="position:relative;top:5px;margin-left:5px;" SRC="image.php" /> 
    <a href="#" id="change">change to another one</a>
<script>
    $(document).ready(function(){ 
        $("#change").click(function(e){
            e.preventDefaut();
            $("img[style="position:relative;top:5px;margin-left:5px;"]").attr('src','new_image.php').load();
        }
   });
</script>

试试这个:

HTML:

<img id="myImage" src="handler.php" alt="" />
<a id="change" href="#" title="change">change image</a>
Jquery:

<script type="text/javascript">
$(document).ready(function(){ 
$("#change").click(function(e){
 e.preventDefaut();
 var hUrl = "handler.php"
 $("#myImage").attr('src', (hUrl + getRandomQueryString()));
 });//click
});//ready
//avoid cache :
function getRandomQueryString()
{
  return "&Id=" + Math.round(new Date().getTime() / 1000);
}