0

ここで紹介されているように、Ajax File Upload を使用しようとしています: http://valums.com/ajax-upload/

ご覧のとおり、スクリプトを初期化するために qq.FileUploader オブジェクトを作成する必要があります。ただし、要素の ID を知らなくても、このオブジェクトを動的に作成できる必要があります。私はこのようなものを作成しようとしました:

var uploader, i = 0;
$(".file-upload").each(function() {
    $e = $(this);
    i++;
    uploader[i] = new qq.FileUploader({
        element: $(this)[0],
        action: 'uploadfile.php',
        allowedExtensions: ['doc', 'docx', 'pdf'],
        multiple: false,
        onComplete: function(id, fileName, responseJSON) {
            $($e).siblings('input').val(responseJSON.newfilename);
        }
    });
});

[i]配列内にオブジェクトを含めることができないため、追加した部分がスクリプトを壊すことを知りました。

このオブジェクトを動的に作成する別の方法はありますか? それらはすべて一意の名前を持つ必要があります。そうしないと、onComplete関数はそれらすべてに対して上書きされます。を使用して実験しましeval()たが、正しく動作させることができないようです。

4

2 に答える 2

2

You have to declare uploader as an array first :

var uploader = [];  

Because you declared the variable without defining it, it has the default value of undefined , and your code was translated into something like undefined[i] which triggers an error.

于 2012-08-19T17:44:04.173 に答える
1

Has to be something like

var uploader = {}; 

or else uploader is null and you cannot assign anything to it.

EDIT:

So there're two opitions, in my opinion, if one wants to have an array than it makes sense to declare one, var uploader = []; and then use the uploader.push() method or define it as an object var uploader = {}; and just do uploader[i] = ....

It is also possible to do the latter with an a array, but in the latter case I see no point in maintaining the counter (i).

于 2012-08-19T17:45:47.140 に答える