验证在验证输入之前显示成功

时间:2013-07-03 12:16:25

标签: javascript jquery ajax

我有一个表单,我正在使用jQuery验证来验证输入是否完整。然而,正在发生的是验证器正在显示消息,但它也正在对boxesadd.php进行Ajax调用。我的印象是这个ajax调用只是在成功验证后才进行的。当输入尚未完成时,为什么它会变为Ajax事件。感谢

jquery代码

$(function () {

    $.validator.setDefaults ({

        errorClass: 'form_error',
        errorElement: 'div'
    });

    $("#BA_boxform").validate({
        rules: {
            BA_customer: {
                required: true
            },
            customerdept: {
                required: true
            },
            customeraddress: {
                required: true
            },
            BA_service: {
                required: true
            },
            BA_box: {
                required: true
            },
            BA_destdate: {
                required: true
            },
            BA_authorised: {
                required: true
            }
        },
        messages: {
            BA_customer: {
                required: '<br />* required: You must select a customer'
            },
            customerdept: {
                required: "<br />* required: You must select a department"
            },
            customeraddress: {
                required: "<br />* required: You must select a customer address"
            },
            BA_service: {
                required: "<br />* required: You must select a service level"
            },
            BA_box: {
                required: "<br />* required: You must enter a box number for intake"
            },
            BA_destdate: {
                required: "<br />* required: You must enter a destruction date"
            },
            BA_authorised: {
                required: "<br />* required"
            }
        }
    });

    $('#BA_boxform').on('submit', function () {

        var formdata = $('#BA_boxform').serialize() + '&submit=' + $(this).val();

        //alert(formdata);
        $.ajax({
            type: "POST",
            url: "/domain/admin/requests/boxes/boxesadd.php",
            data: formdata,
            dataType: 'json',
            success: function (msg) {
                //alert(msg);
                if (typeof msg.boxerrortext !== "undefined" && msg.boxerrortext == "You must enter a box for intake") {
                    $("#BA_addbox").html(msg.boxerrortext);
                } else {
                    $("#BA_addbox").html("You have successfully added box(es) " + '<span style="font-weight: bold;color:black;">' + msg.box + '</span>' + ' to the archive.' + '<br />' + 'You may now close this window or input more boxes.');
                    $("#BA_boxform").get(0).reset();
                }
                //$("#confirm_department").hide();

                /*
               var $dialog = $('<div id="dialog"></div>')
               .html('Your intake was successfully submitted and will be viewable in the reporting area.<br /><br />Thank you.');
               $dialog.dialog({
               autoOpen: true,
               modal: true,
               title: 'Box intake submission successfull',
               width: 400,
               height: 200,
               draggable: false,
               resizable: false,
               buttons: {
               Close: function() {
               $( this ).dialog( "close" );
               }
               }
               });
               */
                //alert(msg);
                //console.log(msg);
                //$("#BA_addbox").html(msg.box);

                //$("#formImage .col_1 li").show();
                //$("#BA_boxform").get(0).reset();
                //$("#boxaddform").hide();
            }
        });
        return false;
    });
});

1 个答案:

答案 0 :(得分:0)

在你进行ajax调用之前检查你的消息是否为空。