我正在尝试使用Securimage PHP脚本(参见google)来执行验证码,但我有一个问题:当我按下链接“不同的图像”(以显示另一个验证码)时,我丢失了在PHP表单字段中输入的所有文本。因此,用户必须重新输入所有内容,这是有问题的。
下面是代码。注意:我去掉了href的“#”,因为出于某种原因,它无法更改验证码图像。有人知道为什么吗?(在Safari和Firefox中都会发生这种情况)
<img id='captcha' src='/myproject/securimage/securimage_show.php' alt='CAPTCHA Image' /><br> <a href='' onclick='document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false'> [Different Image ]</a>发布于 2012-06-05 18:41:07
因为您已经从href中删除了'#‘,这就是为什么当单击链接时整个页面都会刷新,并且所有字段数据都会丢失。不要删除它,然后使用以下代码
<img id='captcha' src='/myproject/securimage/securimage_show.php' alt='CAPTCHA Image' />
<br>
<a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false;"> [Different Image ]</a>发布于 2012-06-05 19:18:53
这不是因为去掉了“#”。页面重新加载,因为您在<a>标记中使用了错误的引号。当将单引号'用于html属性以及onclick中包含的javascript时,浏览器将只对onclick事件使用document.getElementById(。return false;将不会执行,页面将重新加载。
对于html属性,您可以始终使用",对于javascript,您可以始终使用'。或者使用\转义attrbiutes中的相同引号。
这也可以在没有‘#’的情况下工作:
<a href="" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false;"> [Different Image ]</a>或者它的转义版本:
<a href='' onclick='document.getElementById(\'captcha\').src = \'/securimage/securimage_show.php?\' + Math.random(); return false;'> [Different Image ]</a>https://stackoverflow.com/questions/10895661
复制相似问题