Javascript 为JS aray中的每个值创建变量
上传文件到拖放区后,我需要在上传文件列表旁边显示一个表单输入字段,允许将标签名称输入添加到图像中Javascript 为JS aray中的每个值创建变量,javascript,html,forms,Javascript,Html,Forms,上传文件到拖放区后,我需要在上传文件列表旁边显示一个表单输入字段,允许将标签名称输入添加到图像中 $(function(){ var ul = $('#upload ul'); $('#drop a').click(function(){ // Simulate a click on the file input button // to show the file browser dialog $(this).parent(
$(function(){
var ul = $('#upload ul');
$('#drop a').click(function(){
// Simulate a click on the file input button
// to show the file browser dialog
$(this).parent().find('input').click();
});
// Initialize the jQuery File Upload plugin
$('#upload').fileupload({
// This element will accept file drag/drop uploading
dropZone: $('#drop'),
// This function is called when a file is added to the queue;
// either via the browse button, or via drag/drop:
add: function (e, data) {
var tpl = $('<li class="working uploaded"><input type="text" value="0" data-width="48" data-height="48"'+
' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"addTags();return false;");
var i = document.createElement("input"); //input element, text
i.setAttribute('type',"hidden");
i.setAttribute('value',"fileName");
i.setAttribute('name',"username");
var t = document.createElement("input"); //input element, text
t.setAttribute('type',"text");
t.setAttribute('name',"tags");
var s = document.createElement("submit"); //input element, Submit button
s.setAttribute('type',"submit"););
s.setAttribute('name',"login");
s.setAttribute('value',"Add Tags");
f.appendChild(i);
f.appendChild(t);
f.appendChild(s);
document.getElementsById('filesUploaded')[0].appendChild(f);
var imgNameInput = document.createElement("input");
imgNameInput.setAttribute("name","tags");
document.getElementById("imgList").appendChild(imgNameInput);
// Append the file name and file size
tpl.find('p').text(data.files[0].name)
.append('<i>' + formatFileSize(data.files[0].size) + '</i>');
// Add the HTML to the UL element
data.context = tpl.appendTo(ul);
// Initialize the knob plugin
tpl.find('input').knob();
// Listen for clicks on the cancel icon
tpl.find('span').click(function(){
if(tpl.hasClass('working')){
jqXHR.abort();
}
tpl.fadeOut(function(){
tpl.remove();
});
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
},
progress: function(e, data){
// Calculate the completion percentage of the upload
var progress = parseInt(data.loaded / data.total * 100, 10);
// Update the hidden input field and trigger a change
// so that the jQuery knob plugin knows to update the dial
data.context.find('input').val(progress).change();
if(progress == 100){
data.context.removeClass('working');
}
},
fail:function(e, data){
// Something has gone wrong!
data.context.addClass('error');
}
});
// Prevent the default action when a file is dropped on the window
$(document).on('drop dragover', function (e) {
e.preventDefault();
});
// Helper function that formats the file sizes
function formatFileSize(bytes) {
if (typeof bytes !== 'number') {
return '';
}
if (bytes >= 1000000000) {
return (bytes / 1000000000).toFixed(2) + ' GB';
}
if (bytes >= 1000000) {
return (bytes / 1000000).toFixed(2) + ' MB';
}
return (bytes / 1000).toFixed(2) + ' KB';
}
});
HTML是这样做的
<form id="upload" method="post" action="actions/upload.php" enctype="multipart/form-data">
<input type="hidden" name="username" value="<?php echo $username; ?>" id="username">
<div id="drop">
Drop Here
<a>Browse</a>
<input type="file" name="upl" multiple />
</div>
<ul id="filesUploaded">
<!-- The file uploads will be shown here -->
</ul>
</form>
似乎您正在尝试访问DOM中的元素,但它不在那里/尚未加载。你的剧本在头版吗?如果是这样,请等待内容加载$function{},或者将脚本放在body标记的末尾。如果是这种情况,请相应地标记您的问题,并立即停止使用onsubmit之类的内联事件处理程序。您可能还希望放弃直接的DOM操作以支持。包含jQuery然后不真正使用它没有什么意义。事实上,您的代码示例看起来很像您从不同的源复制粘贴它们,而不理解它们的含义。不要这样做。可能重复的我理解有时帮助那些没有很好理解的人很烦人,但出于善意,我想请你回答我的问题。你应该替换js代码来记录ready hander。[香草js上的示例][1][1]: