最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Force click event on input file or open choose file window - Stack Overflow

programmeradmin9浏览0评论

I have a form in my HTML with field.

I'd like to show the file selection window when the user clicks somewhere else (for example, an image on other part of the screen). The reason I want to do this is that I have a form with many fields, posting to some action "x", and the file selection form on other part of screen, submiting to action "y"

<form action="x">
   <input type="text" name="field1">
   <input type="text" name="field2">
   <input type="text" name="field3">
   <img src="select_file.png" onclick="//triggers file selection on other form">
   <input type="text" name="field4">
   <input type="text" name="field5">
</form>

<form action="y">
  <input type="file" name="myfile">
</form>

Is there any other way to do this?

I have a form in my HTML with field.

I'd like to show the file selection window when the user clicks somewhere else (for example, an image on other part of the screen). The reason I want to do this is that I have a form with many fields, posting to some action "x", and the file selection form on other part of screen, submiting to action "y"

<form action="x">
   <input type="text" name="field1">
   <input type="text" name="field2">
   <input type="text" name="field3">
   <img src="select_file.png" onclick="//triggers file selection on other form">
   <input type="text" name="field4">
   <input type="text" name="field5">
</form>

<form action="y">
  <input type="file" name="myfile">
</form>

Is there any other way to do this?

Share Improve this question asked Oct 23, 2010 at 21:15 Daniel CukierDaniel Cukier 12k15 gold badges71 silver badges131 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4
 $("img").click(function(){
    $("input[name=myfile]").trigger('click');
 });

Demo. Click on the envelope image.

Edit: Giving ID's on the each of input fields will make the code more readable and "good."

发布评论

评论列表(0)

  1. 暂无评论