jquery验证和jquery ajax插件表单提交

时间:2018-12-01 20:54:51

标签: jquery jquery-validate jquery-forms-plugin

我有一个应随ajax请求一起提交的表格。我使用jquery表单插件进行ajax调用。一切都好。但是当我使用jquery验证插件验证此表单时,表单提交有效,即使表单无效。 这是我的代码:

$.validator.addMethod('le', function (value, element, param) {
    return this.optional(element) || parseInt(value) >= parseInt($(param).val());
}, 'Invalid value');
$('#educational_records_form').validate({
    errorElement: "h6",
    rules: {
        edu_cert_start_year: {
            required: true,
            number: true

        },
        edu_cert_end_year: {
            required: true,
            number: true,
            le: '[name="edu_cert_start_year"]'
        }
    },
    messages: {
        edu_cert_end_year: {
            le: 'Must be less than bid price.'
        }
    },
    submitHandler:function (form) {
        $(form).ajaxSubmit();
    }
});

和我的表格:

$('#educational_records_form').submit(function () {
    if ($('[name="edu_still_student"]').prop('checked')) {
        $('[name="edu_cert_end_month"]').append('<option value="continued"></option>');
        $('[name="edu_cert_end_month"]').val('continued');
        $('[name="edu_cert_end_year"]').append('<option value="continued"></option>');
        $('[name="edu_cert_end_year"]').val('continued');
    }

    $(this).ajaxSubmit({
        success: function () {

            UIkit.modal('#new_edu_course').hide();
            $('#educational_records_form').resetForm();
            $('#edu_cert_end_box').removeClass('uk-hidden');
            $('#edu_cert_end_box').addClass('uk-animation-fade');
            $('#edu_cert_start_box').removeClass('uk-width-1-1@l');
            $('#edu_cert_start_box').addClass('uk-width-1-2@l');

        },
        fail: function () {
            alert('fail');
        }
    });
    return false;
});

对不起,英语不好。我在这个问题上工作了2周!但是我解决不了。非常感谢。

1 个答案:

答案 0 :(得分:-1)

尝试

$('#educational_records_form').submit(function (e) {
 e.preventDefault();
if($("#educational_records_form").valid()){
if ($('[name="edu_still_student"]').prop('checked')) {
    $('[name="edu_c`enter code here`ert_end_month"]').append('<option value="continued"></option>');
    $('[name="edu_cert_end_month"]').val('continued');
    $('[name="edu_cert_end_year"]').append('<option value="continued"></option>');
    $('[name="edu_cert_end_year"]').val('continued');
}

$(this).ajaxSubmit({
    success: function () {

        UIkit.modal('#new_edu_course').hide();
        $('#educational_records_form').resetForm();
        $('#edu_cert_end_box').removeClass('uk-hidden');
        $('#edu_cert_end_box').addClass('uk-animation-fade');
        $('#edu_cert_start_box').removeClass('uk-width-1-1@l');
        $('#edu_cert_start_box').addClass('uk-width-1-2@l');

    },
    fail: function () {
        alert('fail');
    }
});
}
});