简体   繁体   中英

Javascript .click() not firing in safari

I have a file upload input in my application and I'm using a link and Javascript to open the dialog.

<a class="uploadlink" style="cursor:pointer" onclick="document.getElementById('file').click()">Open</a>

<input type="file" name="file" id="file" style="display:none" onchange="document.getElementById('txt').value = this.value"/>

My code is working in all browsers and devices except Safari and Apple devices. When I click the link and check the link the console doesn'teven register a error. Can someone suggest a solution?

Since I had display:none in safari it was not only not rendered but also not referenced. I have now changed my code as follows.

Jquery solution

<a class="uploadlink" style="cursor:pointer" onclick="$('#file').trigger('click');">Open</a>
<input type="file" name="file" id="file" style="opacity:0" onchange="document.getElementById('txt').value = this.value"/>

Javascript Solution

 <a class="uploadlink" style="cursor:pointer"  onclick="document.getElementById('file').click();">Open</a>

It is working now even in safari.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM