简体   繁体   English

使用Jquery单击隐藏文件上传按钮?

[英]Use Jquery to click a hidden file upload button?

I have a hidden file upload as it looks really bad, I have displayed a nicer looking button and would like it to click the hidden file upload when its clicked. 我有一个隐藏的文件上传,因为它看起来非常糟糕,我已经显示了一个更好看的按钮,并希望它点击隐藏文件上传时单击。

function ClickUpload() {
    $("#FileUpload").trigger('click');
}

<div id="MyUpload">
    <span id="FileName">Choose File</span>
    <input id="uploadButton" type="button" value="Upload" onclick="ClickUpload()"> 
</div>
<div id="hideUglyUpload">
    <input type="file" name="FileUpload" id="FileUpload"/>
</div>

So far i can move into the function ClickUpload() but it just passes through the click without the file selection window popup. 到目前为止,我可以进入函数ClickUpload()但它只是通过单击而没有弹出文件选择窗口。

Strange that it doesn't work. 奇怪的是它不起作用。 Try 尝试

<input id="uploadButton" type="button" value="Upload" onclick='$("#FileUpload").click()'> 

I prefer not to have inline JS function calls in markup ... so a little change... 我不想在标记中使用内联JS函数调用...所以稍微改变一下......

   $(document).ready(function() {
      $('#uploadButton').on('click',function(evt){
         evt.preventDefault();
         $('#FileUpload').trigger('click');
     });
  });

<div id="MyUpload">
    <span id="FileName">Choose File</span>
    <input id="uploadButton" type="button" value="Upload"> 
</div>
<div id="hideUglyUpload">
    <input type="file" name="FileUpload" id="FileUpload"/>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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