機能です:関数が呼び出されるたびにjavascriptで一意のIDを作成するにはどうすればよいですか?ここ
function addAttachment() {
if (allowed_attachments <= 0)
return alert("', $txt['more_attachments_error'], '");
document.getElementById("uploadBtn").onchange = function() {
document.getElementById("uploadFile").value = this.value;
};
setOuterHTML(document.getElementById("moreAttachments"),
\'<div class="upload_attachment">\
<input id="uploadFile" placeholder="File">\
<label class="custom-file-input">\
<input type="file" id="uploadBtn" name="attachment[]">\
</label></div><span id="moreAttachments">\
</span>\');
allowed_attachments = allowed_attachments - 1;
return true;
}
UploadBtnとUploadFileは、私がこれをどのように行うか、ユニーク毎回する必要がありますか?
var UNIQUE_ID = 0; // the ID counter (will be incremented each time a new item is created so it'll be unique)
function addAttachment() {
if (allowed_attachments <= 0)
return alert("', $txt['more_attachments_error'], '");
document.getElementById("uploadBtn").onchange = function() {
document.getElementById("uploadFile").value = this.value;
};
UNIQUE_ID++; // increment the ID counter
setOuterHTML(document.getElementById("moreAttachments"),
'<div class="upload_attachment">\
<input id="uploadFile' + UNIQUE_ID + '" placeholder="File">\
<label class="custom-file-input">\
<input type="file" id="uploadBtn' + UNIQUE_ID + '" name="attachment[]">\
</label></div><span id="moreAttachments' + UNIQUE_ID + '">\
</span>');
allowed_attachments = allowed_attachments - 1;
return true;
}
「ユニークな」どのように一意である必要があります:私は本当にこのような – deceze
短い答え:十分大きな乱数を生成する。 – deceze
[Mongoh's Truly Uniquie ID](https://github.com/mongoh/tuid) –