为什么此表单仍在提交?

时间:2017-07-02 05:32:09

标签: javascript jquery html forms

我真的厌倦了这种形式,我试图阻止它提交一段我不记得了!

我试图阻止表单提交,如果验证不正确,我相信它在提交时正在寻找文件codes.txt

请有人告诉我,只有在我的验证正确后才能提交?

帮我堆叠溢出!你是我最后的希望。

        $('form').submit(function() {
            var errors = 0;
            $("form input[required]").map(function(){
                if( !$(this).val() ) {
                    errors++;
                }
            });

            $("form textarea[required]").map(function(){
                if( !$(this).val() ) {
                    errors++;
                }
            });

            if(errors > 0){
                alert("You must fill out the required fields");
                return false;
            }

            if(!$('#terms-checkbox').is(':checked')) {
                alert('You must accept the terms and conditions');
                return false;
            } else {
                if ($.trim($('#code').val()).length == 0){
                    alert("You must enter code");
                } else {
                    $.get('codes.txt', function (contents) {
                        if (contents.includes($('#code').val())) {
                            validForm =true;
                        } else {
                            alert("Sorry!! That is not a valid code.");
                            return false;
                        }
                    });
                }
            }
        });
    <script type="text/javascript">var validForm=false;</script>
    <iframe name="hidden_iframe" id="hidden_iframe" style="display:none;" onload="if(validForm) {window.location='confirmation-page.html'}"></iframe>


    <form action="https://docs.google.com/forms/d/e/1FAIpQLSdC9Jr65fCIhRLiSXzVRoDKJAB8was3LoyN0DaCVrVLRa-nJg/formResponse" target="hidden_iframe" method="post">


<!--Some Inputs goes here-->

        <input id="terms-checkbox" type="checkbox" name="aaaa" value="Bike"> <span style="color: red;font-size: 14px;">*</span> I have read and agree to the above terms and conditions

        <input type="submit" value="Send" class="form-control btn-default" >

    </form>

0 个答案:

没有答案