为了账号安全,请及时绑定邮箱和手机立即绑定

Javascript 表单在提交时不重定向

Javascript 表单在提交时不重定向

ITMISS 2023-05-11 14:30:14
我的表单正在调用以下函数。它在桌面上运行,用户被定向到下载确认/失败页面。但是,在移动设备上(在 iOS 和 iPad 上测试过),用户不会被重定向。任何人都可以阐明一下吗?功能function submit_form(){    var email = document.getElementById("main-email-field").value    // todo email validation    if (email.length > 7)    {        window.location = '/download-confirmation'    }    else    {        window.location = '/download-failed'    }    return false;}形式<form id="main-email-form" class="email-form" method="POST" onsubmit="submit_form()" action="https://europe-west1-hoddle-website.cloudfunctions.net/registerUser" target="hiddenFrame">    <input id="main-email-field" class="main-email-field" type="email" name="email" placeholder="Email address">    <button id="main-email-button" class="main-email-button">Download</button>    <input type="hidden" id="main-referralID-field" name="referralID" value="">    <input type="hidden" id="main-referralType-field" name="referralType" value="">    <input type="hidden" id="main-referralUserID-field" name="referralUserID" value="">    </form>        <iframe id="hiddenFrame" class="hiddenFrame" name="hiddenFrame" width="0" height="0" border="0" style="display: none;"></iframe>
查看完整描述

1 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

尝试这种超时和 preventDefault 的组合


window.addEventListener("load", function() {

  document.getElementById("main-email-form").addEventListener("submit", function(e) {

    const email = document.getElementById("main-email-field").value

    // todo email validation

    console.log(email,email.length); 

    if (email.length > 7) {

      setTimeout(function() {

        console.log("Download allowed");

        // window.location = '/download-confirmation'; // uncomment after testing

      }, 2000)

    } else {

      e.preventDefault(); // stop submission

      console.log("Download failed");

      // window.location = '/download-failed'; // uncomment after testing

    }

  })

})

<form id="main-email-form" class="email-form" method="POST" action="https://europe-west1-hoddle-website.cloudfunctions.net/registerUser" target="hiddenFrame">

  <input id="main-email-field" class="main-email-field" type="email" name="email" placeholder="Email address">

  <button type="submit" id="main-email-button" class="main-email-button">Download</button>

  <input type="hidden" id="main-referralID-field" name="referralID" value="">

  <input type="hidden" id="main-referralType-field" name="referralType" value="">

  <input type="hidden" id="main-referralUserID-field" name="referralUserID" value="">

</form>


<iframe id="hiddenFrame" class="hiddenFrame" name="hiddenFrame" width="0" height="0" border="0" style="display: none;"></iframe>


查看完整回答
反对 回复 2023-05-11
  • 1 回答
  • 0 关注
  • 119 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信