我有一个按钮作为上传文件按钮,除了Safari之外的所有浏览器都能正常工作,你可以在这里看到:
如果您点击"联系我们"和表格中的附件文件,它不起作用。
HTML
<label for="file" style="display:none">Attach file:</label>
<input type="file" id="file" name="file" accept="image/jpg, image/jpeg, image/gif, image/png, application/pdf, .txt, application/pdf, application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.slideshow, application/vnd.openxmlformats-officedocument.presentationml.presentation" class="multi" maxlength="1" />
<button id="fileSelect">attach file (max. 2MB)</button>
JQUERY
document.querySelector('#fileSelect').addEventListener('click', function(e) {
// Use the native click() of the file input.
document.querySelector('#file').click();
}, false);