2010-02-20 67 views
0

我對JavaScript和jQuery相當陌生。我正在使用Uploadify將圖像上傳到服務器。頁面上有多個Uploader對象。我需要將正在使用的上傳器對象的ID傳遞給服務器。這是我到目前爲止有:jQuery中的動態腳本數據Uploadify

$('input.ImageUpload').uploadify({ 
    ... 
    'scriptData': {'id': ??????}, 
    ... 
}); 

我怎樣才能做到這一點?我試過「this.id」和「$(this).attr('id')」。我是否以正確的方式去解決這個問題?

回答

2

我不知道這是否是理想的,但是這是我落得這樣做:

$('input.ImageUpload').each(function() { 
    $(this).uploadify({ 
    ... 
    scriptData({'id': $(this).attr('id')}, 
    ... 
    }); 
}); 
1

這是我如何使用它成功:

$(document).ready(function() { 
    $(".upload").each(function() { 
     $(this).uploadify({ 
      'uploader'  : '/code/js/jquery.uploadify/example/scripts/uploadify.swf', 
      'script'  : '/code/js/jquery.uploadify/example/scripts/uploadify.php', 
      'cancelImg'  : '/code/js/jquery.uploadify/example/cancel.png', 
      'folder'  : '<?=$upload_path?>' + $(this).attr('id'), 
      'queueID'  : 'fileQueue', 
      'auto'   : true, 
      'multi'   : false, 
      'scriptData'  : ({'var_name': 'this_var_value'}), 
      'fileDesc'   : 'Images and PDF files only! (JPG, PNG, PDF)', 
      'fileExt'    : '*.pdf;*.jpg;*.jpeg;*.png', 
      'buttonText'  : 'browse' + $(this).attr('id') 
     }); 
    }); 
}); 



<div id="fileQueue"></div> 
<input class="upload" type="file" name="ImageUpload" id="_ID_1" /><br /> 
<input class="upload" type="file" name="ImageUpload" id="_ID_2" /><br /> 
<input class="upload" type="file" name="ImageUpload" id="_ID_3" /><br />