开发者

AJAX replaceWith and Jquery validation

开发者 https://www.devze.com 2023-03-01 08:33 出处:网络
I have a 5 page form that\'s swapping out just the form using replaceWith on each submit. It\'s all working great except for the validation on the final page. It seems like it\'s just not doing the

I have a 5 page form that's swapping out just the form using replaceWith on each submit.

It's all working great except for the validation on the final page. It seems like it's just not doing the document.ready (which was inserted with page0). I can get an alert to show on page0, but then the subsequent html, added by replaceWith, doesn't validate.

Thanks in advance for any help you can give.

function sendTo(where) {        
var datastring = $("#QouteForm").serialize();

    //show the loading sign
    $('.loading').show();

$.ajax({
        url: where,
        type: "GET",
        data: datastring, 
        cache: false,
        success: function (html) {             
            //hide the form
            $('#formdiv').fadeOut('slow',complete);
            function complete() { 
                $('#formdiv').fadeIn('slow').replaceWith(html); 
                $('.loading').hide();
            }
        }
    });
retur开发者_如何学Cn false;
}

$("#QouteForm").validate({
    submitHandler: function(form) {
$('#QouteForm').submit();
    }
});

//custom method to check if the field is equal to its default value
$.validator.addMethod(
    'notEqual', function (value, element) {
        return value !== element.defaultValue;
    }, 'Please enter a value.'
);

//method to check if it's a valid US phone number
jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
    return this.optional(element) || phone_number.length > 9 &&
    phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
});

//do the validation
$(function() {
$("#QouteForm").validate({  
    rules: {
        firstname: "notEqual",
        lastname: "notEqual",
        company: "notEqual",
        phone: {
            required: true,
            phoneUS: true
        },
        email: {
            required: true
        }
    }

});
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号