Ajax表单提交两次?

时间:2015-02-05 04:05:57

标签: javascript php jquery ajax symfony

我找不到错误,但提交表格两次。请帮助我在这里错过了什么。

    $('#supplierForm').on('submit', function(e) {
                e.preventDefault();
                if ($(this).valid()) {
                    $.ajax({
                        async: false,
                        data: $("#supplierForm").serialize(),
                        url:  '{{ url('supplier_edit_new') }}',
                        type: 'POST',
                        success: function (data) {
                                window.location.reload();

                        }
                    });
                }
                return false;
            })

;

用于验证脚本

$("document").ready(function(){

    $('#supplierForm').validate({
        errorClass: 'help-block',
        rules: {
            'line1': "required",
            'line2': "required",
            'suburb': "required",
            'state' : "required",
            'country':"required",


        },
        messages: {

            'line1':{required: "Please enter supplier's address."},
            'line2':{required: "Please enter supplier's address."},
            'suburb':{required: "Please enter  supplier's suburb."},
            'state':{required: "Please select a state"},
            'country':{required: "Please enter country"},
            'postcode':{required: "Please enter postcode"},

        },

        highlight: function (element) {
            $(element).parent().parent().removeClass("success").addClass("error");
        },

        unhighlight: function (element) {
            $(element).parent().parent().removeClass("error").addClass("success");
        }
    }); // validate
});

对于html

{{ form_widget(form.submit,{'attr':{'class':'btn btn-primary btn-large btn-style','value':'Save changes'} }) }} 
it generate 

<button id="ovc_bundle_productbundle_supplier_submit" class="btn btn-primary btn-large btn-style" value="Save changes" name="ovc_bundle_productbundle_supplier[submit]" type="submit">Save / Update Details</button>

1 个答案:

答案 0 :(得分:3)

尝试此操作以防止多次绑定事件,

$('#supplierForm').off('submit');
$('#supplierForm').on('submit', function(e) {
            e.preventDefault();
            if ($(this).valid()) {
                $.ajax({
                    async: false,
                    data: $("#supplierForm").serialize(),
                    url:  '{{ url('supplier_edit_new') }}',
                    type: 'POST',
                    success: function (data) {
                            window.location.reload();

                    }
                });
            }
            return false;
        })