You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

登录页Captcha验证通过后,点击登录按钮无法HTML/JS重定向的问题

问题原因及解决方案

导致重定向失效的核心问题有两个:

1. 按钮的submit类型触发了表单默认提交行为

你的按钮是type="submit",点击时会自动触发所在表单的默认提交流程——页面会发起表单提交请求并刷新,这个行为会直接中断或覆盖JavaScript的重定向操作,导致location.replace无法生效。

2. Location的大小写错误

JavaScript中全局对象的位置属性是小写的window.location(可省略window),大写的Location不是标准的全局属性,写法错误。


修复方案(二选一即可)

方案一:修改按钮类型为button

把按钮的type从submit改为button,这样点击只会执行onclick绑定的函数,不会触发表单提交:

<button type="button" id="log" name="log" onclick="redirect()"> Log in here</button><br><br>

<Center>
<b>Captcha Verification : </b><br>
<div class="capt"> 
<center><h3 type="text" id="mainCaptcha"></h3></center>
<script>
function redirect(){
    if (ValidateCaptcha()) {
          window.location.replace("redirect.html");
          // 也可以使用 window.location.href = "redirect.html";
        } else {
          alert("Please complete the captcha to log in."); 
        }
}
</script>

方案二:保留submit类型,阻止默认提交行为

如果必须保留type="submit",需要在函数中阻止表单的默认提交动作:

<button type="submit" id="log" name="log" onclick="redirect(event)"> Log in here</button><br><br>

<Center>
<b>Captcha Verification : </b><br>
<div class="capt"> 
<center><h3 type="text" id="mainCaptcha"></h3></center>
<script>
function redirect(event){
    // 阻止表单默认提交行为
    event.preventDefault();
    if (ValidateCaptcha()) {
          window.location.replace("redirect.html");
        } else {
          alert("Please complete the captcha to log in."); 
        }
}
</script>

内容的提问来源于stack exchange,提问作者Siddhi Munjal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 07:05:25