当前位置: 动力学知识库 > 问答 > 编程问答 >

events - jquery: firing click() on input:file?

问题描述:

I'm currently trying to restyle (eclipse) a fileupload element on my website with this reference. All well and the result is promising, when I put the fileupload element below my new alternative image (as instructed on afore mentioned tutorial). Now I want to place the fileinput element somewhere else and bind a click function on the image to a click on the fileinput element to fire a file browser dialog.

Alas the usual $('input:file').click() or $('input:file').trigger('click') is not working.

I'm also interested in other ways to fire a file dialog, without a file upload element (it is only for javascript processing, no upload to the server).

Oh it does not need to work on any version of IE.

网友答案:

Missing a quote on both your code samples, try $('input:file').trigger('click') However, as far as I know, it's not possible to do this, at least this way. It's by design that way, on purpose, for security issues.

You can try to do it by tracking mouse movement and move the hidden file input on top of the new image or whatever you will use to style the input.

网友答案:

How are you doing it right now, can you give us some more code? Also try adding a unique ID to the file input and the field you want to trigger. For example:

<span id="browse">Click here to choose a file</span>
<input type="file" id="file" />

Then try doing:

$('#browse').bind('click', function(e) {
  $('#file').click();
});
网友答案:
<div class="imageUploadInput">
    <img id="uploadImage" src="images/camera_icon.png" />
    <input type="file" name="thumb" id="uploadImageInput" />
</div>

jQuery:

$('#uploadImage').livequery("click",function(){
    $('#uploadImageInput').trigger('click');
});

or

$('#uploadImage').click(function(){
   $('#uploadImageInput').trigger('click');
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: