JS无线电按钮仅值提交一个结果

发布于 2025-02-10 09:28:05 字数 1976 浏览 2 评论 0原文

我的表单设置以运行一个函数,此功能将根据无线电按钮选择将HREF更改为下一页。 由于某种原因,该价值没有被提交,当它确实提交时,它只是作为VoIP出现。

evalportalp1.html:

<!DOCTYPE html>
<html lang="en">
    <head>
    </head>
    <body>
        <form id="myForm" onsubmit="goPThree(event)" method="get">
            <div id="pBFContainer">
                <div id="bodyFOption1">
                    <label for="email">Enter email address for service confirmation:<p></label>
                    <input type='email' id='inputEmail' name='email' minlength="10" size="70" required></input>
                </div>
                <div id="bodyFOption2">
                    <label for="testType">Choose the test theme for your evaluation:<p></label>
                    <input class="optionT" type='radio' name='testType' value='voip' checked>VoIP Readiness<p>
                    <input class="optionT" type='radio' name='testType' value='bandwidth'>Bandwidth quality, user experience, throughput, and capacity.<br><br>
                </div>
            </div>
            <input type="submit" id="subButton" value="Next..." />
        </form>
    </body>
    <script type="text/javascript" src="evalportalp1.js"></script>
</html>

evalportalp1.js:

var ls = window.location.search;
var qs = new URLSearchParams(ls);
var testType = qs.get("testType");

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

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

    return false;
}

I have my form set to run a function and this function will change the href to the next page based on the Radio buttons selection.
for some reason, the value is not being submitted, and when it does submit it's only coming up as VoIP.

evalportalp1.html:

<!DOCTYPE html>
<html lang="en">
    <head>
    </head>
    <body>
        <form id="myForm" onsubmit="goPThree(event)" method="get">
            <div id="pBFContainer">
                <div id="bodyFOption1">
                    <label for="email">Enter email address for service confirmation:<p></label>
                    <input type='email' id='inputEmail' name='email' minlength="10" size="70" required></input>
                </div>
                <div id="bodyFOption2">
                    <label for="testType">Choose the test theme for your evaluation:<p></label>
                    <input class="optionT" type='radio' name='testType' value='voip' checked>VoIP Readiness<p>
                    <input class="optionT" type='radio' name='testType' value='bandwidth'>Bandwidth quality, user experience, throughput, and capacity.<br><br>
                </div>
            </div>
            <input type="submit" id="subButton" value="Next..." />
        </form>
    </body>
    <script type="text/javascript" src="evalportalp1.js"></script>
</html>

evalportalp1.js:

var ls = window.location.search;
var qs = new URLSearchParams(ls);
var testType = qs.get("testType");

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

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

    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静赏你的温柔 2025-02-17 09:28:05

每当提交时,您需要获取testType字段的值。否则,您将仅阅读当前在URL中的值。

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

  const formData = new FormData(event.target);
  const testType = formData.get("testType");

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

  return false;
}

You need to get the value of the testType field whenever you submit. Otherwise you would just be reading the value thats currently in the URL.

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

  const formData = new FormData(event.target);
  const testType = formData.get("testType");

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

  return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文