Javascript 表单在提交时不重定向

我的表单正在调用以下函数。它在桌面上运行,用户被定向到下载确认/失败页面。


但是,在移动设备上(在 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>


ITMISS
浏览 120回答 1
1回答

梵蒂冈之花

尝试这种超时和 preventDefault 的组合window.addEventListener("load", function() {&nbsp; document.getElementById("main-email-form").addEventListener("submit", function(e) {&nbsp; &nbsp; const email = document.getElementById("main-email-field").value&nbsp; &nbsp; // todo email validation&nbsp; &nbsp; console.log(email,email.length);&nbsp;&nbsp; &nbsp; if (email.length > 7) {&nbsp; &nbsp; &nbsp; setTimeout(function() {&nbsp; &nbsp; &nbsp; &nbsp; console.log("Download allowed");&nbsp; &nbsp; &nbsp; &nbsp; // window.location = '/download-confirmation'; // uncomment after testing&nbsp; &nbsp; &nbsp; }, 2000)&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; e.preventDefault(); // stop submission&nbsp; &nbsp; &nbsp; console.log("Download failed");&nbsp; &nbsp; &nbsp; // window.location = '/download-failed'; // uncomment after testing&nbsp; &nbsp; }&nbsp; })})<form id="main-email-form" class="email-form" method="POST" action="https://europe-west1-hoddle-website.cloudfunctions.net/registerUser" target="hiddenFrame">&nbsp; <input id="main-email-field" class="main-email-field" type="email" name="email" placeholder="Email address">&nbsp; <button type="submit" id="main-email-button" class="main-email-button">Download</button>&nbsp; <input type="hidden" id="main-referralID-field" name="referralID" value="">&nbsp; <input type="hidden" id="main-referralType-field" name="referralType" value="">&nbsp; <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>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript