ScriptData الديناميكي في تحميل jQuery
-
21-09-2019 - |
سؤال
أنا جديد جدًا على JavaScript و JQuery. أنا أستخدم تحميل لتحميل الصور على الخادم. هناك العديد من كائنات التحميل على الصفحة. أحتاج إلى تمرير معرف كائن التحميل الذي يتم استخدامه للخادم. هذا ما لدي حتى الآن:
$('input.ImageUpload').uploadify({
...
'scriptData': {'id': ??????},
...
});
كيف يمكنني تحقيق هذا؟ لقد جربت "this.id" و "$ (this) .attr ('id')". هل أنا حتى أذهب إلى هذا بالطريقة الصحيحة؟
المحلول
لست متأكدًا مما إذا كان هذا مثاليًا ، لكن هذا ما انتهى بي الأمر:
$('input.ImageUpload').each(function() {
$(this).uploadify({
...
scriptData({'id': $(this).attr('id')},
...
});
});
نصائح أخرى
هذه هي الطريقة التي أستخدمها بنجاح:
$(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 />
لا تنتمي إلى StackOverflow