jquery ajaxform global functions

jquery ajaxform global functions

本文关键字:functions global ajaxform jquery      更新时间:2023-09-26

我们在网站上使用jQuery表单插件。我们需要能够在所有表单上提交时显示加载 gif。到目前为止,我已经能够像下面这样完成这项工作

    $("#uploadAssetImageForm").ajaxForm({
        dataType: 'json',
        beforeSubmit: function () {Cvrs.SetLoading(true, '#uploadAssetImageForm')},
        success: function(asset) {
            Cvrs.SetLoading(false, '#uploadAssetImageForm');
        $("#uploadAssetImageDiv").dialog("close");
            $(":input", "#uploadAssetImageDiv").not(':button, :submit, :reset').val('').removeAttr('checked').removeAttr('selected');
            ReplaceAsset(asset);
        }
    });

特别是我正在谈论的部分是 beforeSubmit 函数和再次调用以在成功时将其设置为 false。

设置加载仅显示加载 gif 和禁用的表单按钮

function(isLoading, form) {
isLoading ? $(form + ' input[type="submit"]').attr("disabled", true) :
    $(form + ' input[type="submit"]').removeAttr("disabled");

isLoading ? $(form + ' .loading-animation').show() :
    $(form + ' .loading-animation').hide();

有什么方法可以调用 ajaxform 以在 beforeSubmit 上运行该代码并成功以及可能存在的任何其他代码(不过首先运行加载内容)还是我必须经历每种情况并添加它?

我知道我必须在每个表单中放置加载引用,但这是有道理的,因为不同的表单可能需要在不同位置加载 gif

您可以考虑使用 ajaxSend 事件。它在触发 AJAX 发送事件之前触发:

$(document).bind('ajaxSend', function(e, request, options) {
    Cvrs.SetLoading(true, '#uploadAssetImageForm');
});

我希望它有所帮助!