我找不到错误,但是它两次提交了表格。请帮我我错过的一切。

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

最佳答案

尝试这样做以防止多次绑定事件,

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

关于javascript - Ajax表单提交两次?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28336029/

10-13 09:10