简体   繁体   English

Ajax表单提交两次?

[英]Ajax form submitting twice?

I couldn't find the mistake but it submit form twice . 我找不到错误,但是它两次提交了表格。 Please help me what i missed in this . 请帮我我错过的一切。

    $('#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;
            })

;

For validating script 用于验证脚本

$("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
});

For html 对于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>

Try this to prevent binding the event multiple times, 尝试这样做以防止多次绑定事件,

$('#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;
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM