可以在哪个函数Prevent Default for jquery.ajaxForm中完成?

时间:2017-03-22 04:38:40

标签: jquery ajaxform

我正在通过jquery.ajaxForm提交表单。但是由于双击鼠标,表单被提交了两次,我想防止默认单击按钮,但是在$ .ajaxForm中哪个事件可以防止默认?

$('#counter-entry-form').ajaxForm({

            beforeSubmit: function () {
                $('#counter-entry-form').removeData("validator").removeData("unobtrusiveValidation");
                $.validator.unobtrusive.parse($('#counter-entry-form'));
                if ($("#counter-entry-form").valid()) {
                    $('#submitting-btn').attr('disabled','disabled');
                } else {
                    return false;
                }
            },
            success: function (result) {
                if (result.Success) {
                    window.open('@Url.Content("~/Counter/PrintViewForSato?itemId=")' + result.ValueTwo, '_blank');
                    $('#IsFromNameSave').attr('checked', false);
                    $('#IsToNameSave').attr('checked', false);
                    $('#submitting-btn').removeAttr('disabled');
                    if ($('#IsBulkEntry:checked').is(':checked')) {
                        $('#BulkEntryCount').val(result.ReturnId);
                        $('#BulkEntryTotalAmount').val(result.ValueOne);
                        $('#ItemId').val('');
                        $('#ToName').val('');
                        $('#ToDestinationLocal').val('');


                    } else {

                        $('#ItemId').val('');
                        $('#Weight').val(0);
                        $('#FromName').val('');
                        $('#FromDestinationLocal').val('');
                        $('#ToDestinationLocal').val('');
                        $('#ToName').val('');
                        $('#Weight').change();

                    }
                    if ($('#IsReset:checked').is(':checked')) {
                        $('#BulkEntryTotalAmount').val(0);
                        $('#BulkEntryCount').val(0);
                    }
                } else {
                    WorkForce.loader.hide();
                    ShowMessage("error", "error", result.Msg);
                }
            },
            error: function (err) {
                WorkForce.loader.hide();
                if (err.statusText == 'Unauthorized') {
                    ShowMessage('error', 'error', 'Please Login to Continue !!');
                } else {
                    ShowMessage('error', 'error', 'Error !!');
                }
            }
        });

1 个答案:

答案 0 :(得分:0)

您必须在beforeSubmit上取消提交按钮,并在成功或错误时启用它。假设您的提交按钮ID为'counter-entry-form-submit-btn'

$('#counter-entry-form').ajaxForm({
    beforeSubmit: function () {
    $('counter-entry-form-submit-btn').attr('disabled','disabled');
    // your code is here
    },
    success: function () {
    $('#counter-entry-form-submit-btn').removeAttr('disabled');
    // your code is here
    },
    error: function () {
    $('#counter-entry-form-submit-btn').removeAttr('disabled');
    // your code is here
    }
});