0

我需要使用 访问元素id的。我有几个相同的,这就是我需要访问的原因,但似乎我无法使用. 我在,和中都需要这个值。formclass="ajax-upload-image"formsclassid$(this)beforeSenduploadProgresscomplete

有没有办法保存自定义变量,或者通过data发送来提取它?我当前的“解决方案”是使用上次访问的 id 存储一个全局变量,但这几乎不是一个解决方案,因为可以在前一个完成上传之前使用表单,这意味着全局变量的更改。

到目前为止我的剥离代码(没有全局变量):

$('.ajax-upload-image').ajaxForm({
    delegation: true,
    dataType: 'json',

    beforeSend: function() {
        // Some code
    },

    uploadProgress: function(event, position, total, percentComplete) {
        // Some code
    },

    complete: function(data) {
        // Some code
    }
});

非常感谢任何帮助,因为我似乎在这一点上非常卡住。

4

1 回答 1

3

当你打电话时ajaxSubmit(),你可以传递一个context:选项。所有回调都将绑定到此上下文:

$(".ajax-upload-image").submit(function() {
    $(this).ajaxSubmit({
        context: this,
        ...
    });
});

然后,您将能够在回调中使用$(this)this.id来引用已提交的表单。

于 2014-06-29T16:51:31.493 回答