表单将验证或重定向,但不能同时验证或重定向

时间:2017-06-23 15:19:52

标签: javascript jquery html forms

我在这些论坛上搜索并尝试了许多不同的解决方案,但我只是无法找到一种方法让我的表单验证名字,姓氏和电子邮件字段,然后重定向。我可以让它做一个或另一个,但不是两个。

以下是我的代码。我是javascript的新手,但我认为我已接近完成这项工作了:

    <script>
    $.validator.setDefaults({
        submitHandler: function() {
            alert("submitted!");
        }
    });

    $().ready(function() {
        // validate the comment form when it is submitted
        $("#commentForm").validate();

        // validate signup form on keyup and submit
        $("#signupForm").validate({
            rules: {
                firstname: "required",
                lastname: "required",
                email: {
                    required: true,
                    email: true
                },

    });

    $("#signupForm").submit(function(event){
        window.location = 'http://www.lifebridgehealth.org/campaign/newneighborsdownload.aspx';
    });
    </script>

</script>

    <style>
    #commentForm {
        width: 500px;
    }
    #commentForm label {
        width: 250px;
    }
    #commentForm label.error, #commentForm input.submit {
        margin-left: 253px;
    }
    #signupForm {
        width: 670px;
    }
    #signupForm label.error {
        margin-left: 10px;
        width: auto;
        display: inline;
    }
    #newsletter_topics label.error {
        display: none;
        margin-left: 103px;
    }
    </style>



<body>
<div id="main">
    <form class="cmxform" id="commentForm" method="get" action="">
        <fieldset>
            <p>
            <input type="hidden" class="reference" id="HC4__Inquiry__c.RecordTypeId" name="HC4__Inquiry__c.RecordTypeId" value="01246000000tqltAAA" />
    <input type="hidden" class="picklist" id="HC4__Inquiry__c.HC4__Status__c" name="HC4__Inquiry__c.HC4__Status__c" value="Open" />
    <input type="hidden" class="string" id="HC4__Inquiry__c.HC4__Subject__c" name="HC4__Inquiry__c.HC4__Subject__c" value="DG_LBH_PRIM_NewMovers_CY17 form submission" />
    <input type="hidden" class="picklist" id="Patient.LeadSource" name="Patient.LeadSource" value="Web Form: DG_LBH_Primary Care_New Movers" />
    <input type="hidden" class="picklist" id="Patient.HC4__MostRecentLeadSource__c" name="Patient.HC4__MostRecentLeadSource__c" value="Web Form: DG_LBH_Primary Care_New Movers_FY17" />
    <input type="hidden" class="string" id="DemandConnectForm" name="DemandConnect.Form" value="DG_LBH_PRIM_NewMovers_CY17" />
    <label class="string" id="Patient.FirstName_label" for="Patient.FirstName">First Name</label>
    <input type="text" class="string" id="Patient.FirstName" name="Patient.FirstName" value="" required/><br/>
    <label class="string" id="Patient.LastName_label" for="Patient.LastName">Last Name</label>
    <input type="text" class="string" id="Patient.LastName" name="Patient.LastName" value="" required/><br/>
    <label class="email" id="Patient.Email_label" for="Patient.Email">Email</label><input type="text" class="email" id="Patient.Email" name="Patient.Email" value="" required/><br/>
    <label class="string" id="Patient.PostalCode_label" for="Patient.PostalCode">Mailing Zip/Postal Code</label>
    <input type="text" class="string" id="Patient.PostalCode" name="Patient.PostalCode" value="" /><br/>
    <label class="phone" id="Patient.Phone_label" for="Patient.Phone">Home Phone</label>
    <input type="text" class="phone" id="Patient.Phone" name="Patient.Phone" value="" data-fv-field="phonenumber"/><br/>
    <label class="phone" id="Patient.MobilePhone_label" for="Patient.MobilePhone">Mobile Phone</label>
    <input type="text" class="phone" id="Patient.MobilePhone" name="Patient.MobilePhone" value="" data-fv-field="phonenumber"/><br/>
    <label class="picklist" id="HC4__Inquiry__c.HC4__PreferredContactMethod__c_label" for="HC4__Inquiry__c.HC4__PreferredContactMethod__c">Preferred Contact Method</label>
    <select type="picklist" class="picklist" id="HC4__Inquiry__c.HC4__PreferredContactMethod__c" name="HC4__Inquiry__c.HC4__PreferredContactMethod__c" />
    <option value="Home Phone">Home Phone</option>
    <option value="Mobile Phone">Mobile Phone</option>
    </select>
    <br/>
    <label class="picklist" id="HC4__Inquiry__c.Best_Time_to_Reach_You__c_label" for="HC4__Inquiry__c.Best_Time_to_Reach_You__c">Best Time to Reach You</label>
    <select type="picklist" class="picklist" id="HC4__Inquiry__c.Best_Time_to_Reach_You__c" name="HC4__Inquiry__c.Best_Time_to_Reach_You__c" />
    <option value="Morning">Morning</option>
    <option value="Afternoon">Afternoon</option>
    <option value="Evening">Evening</option> 
    </select>
    <br/>
        <label class="boolean" id="HC4__Inquiry__c.Call_Me_Back_to_Schedule_an_Appointment__c_label" for="HC4__Inquiry__c.Call_Me_Back_to_Schedule_an_Appointment__c">Please Call Me Back to Schedule an Appointment</label>
    <input type="checkbox" class="boolean" id="HC4__Inquiry__c.Call_Me_Back_to_Schedule_an_Appointment__c" name="HC4__Inquiry__c.Call_Me_Back_to_Schedule_an_Appointment__c" /><br/>
    <input type="submit" value="Submit" />
        <br/>

    <label class="boolean" id="Patient.WantstoLearnMoreAboutLBH__c_label" for="Patient.WantstoLearnMoreAboutLBH__c" >Learn More About LBH?</label>
    <input type="checkbox" class="boolean" id="Patient.WantstoLearnMoreAboutLBH__c" name="Patient.WantstoLearnMoreAboutLBH__c" checked />
    <br/>
            </p>
            </p>
        </fieldset>
    </form>
</div>
<script>
$("#DG_LBH_PRIM_NewMovers_CY17").validate();
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您应该在提交函数$("#signupForm").submit(...

中执行验证

然后,提交函数通常需要返回true或false。 True将触发表单提交,false将阻止提交。

如果您想在其他地方重定向,最好完全覆盖默认功能。通过在提交事件上调用preventDefault来执行此操作。

$("#signupForm").submit(function(event){ event.preventDefault()...

然后,您可以执行验证并在必要时重定向