开发者

jquery: firing click() on input:file?

开发者 https://www.devze.com 2023-03-12 23:45 出处:网络
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 imag

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 ja开发者_如何学编程vascript 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');
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号