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

JS表单提交Radio按钮取值始终为VoIP问题排查

问题根因

代码存在3处逻辑错误,直接导致单选值读取异常、跳转逻辑失效:

  • 取值时机完全错误:脚本加载阶段就提前读取了当前页面URL的testType查询参数,这个值是页面打开时的初始URL参数,和用户提交表单时选中的单选按钮值没有任何关系,无论用户怎么选,代码用的都是页面加载时拿到的旧值。
  • switch分支缺失break关键字:就算正确拿到了选中值,匹配到对应分支后没有break终止逻辑,会直接穿透执行后续所有分支代码,跳转逻辑会被后面的分支覆盖。
  • 拼接的查询参数为旧值:跳转时拼接的ls是页面加载时的旧查询串,不是用户本次填写表单生成的新数据,就算跳转到目标页面,携带的邮箱、测试类型参数也是错误的。
修复方法

删除脚本顶层提前声明的ls、qs、testType三个全局变量,在表单提交触发时,直接从DOM中读取用户当前选中的单选值、填写的邮箱内容,生成正确的查询参数,同时给switch每个分支补上break。

修复后的evalportalp1.js完整代码

function goPThree(event) {
    event.preventDefault();

    // 获取用户当前选中的测试类型
    const selectedTestType = document.querySelector('input[name="testType"]:checked').value;
    // 获取用户输入的邮箱
    const userEmail = document.getElementById('inputEmail').value;
    // 编码拼接合法的查询参数
    const validQuery = `?email=${encodeURIComponent(userEmail)}&testType=${encodeURIComponent(selectedTestType)}`;

    switch (selectedTestType) {
        case "voip":
            console.log("testtype is voip");
            window.location.href = "evalportalv3.html" + validQuery;
            break;
        case "bandwidth":
            console.log("testtype is bandwidth");
            window.location.href = "evalportalb3.html" + validQuery;
            break;
        default:
            alert("Please pick a valid Option");
            break;
    }

    return false;
}

补充:用encodeURIComponent处理参数值是为了避免邮箱里的特殊字符导致URL解析异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 05:18:19