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

javascript - html input file click event submit form before select file - Stack Overflow

programmeradmin2浏览0评论

I am trying to implement upload file , I want to call the file input to select the file first, after selected the file , then will do form submit. However in my code , it will call form submit immediately. I have tried use ajax but not work. Any way I want to do the form submit just after the user have selected the file. But it seems cannot find a way to wait the finishing of the file input click function ( I mean until the user have selected a file)

Here is my html:

 <input type="text" id="body" name="body" >
 <button type="submit"  id="sent_1" value="Send">Submit</button>         
 <input type="file" id="image_file" name="image" size="chars">
 <button  id="send_img_btn" value="image">Image</button></td>

Here is my javascript:

    $('#send_img_btn').click(function(e){
          e.preventDefault();
    $('#image_file').click();
    $('#form').submit();
});

function imageClick(){
       $('#image_file').click();
}

function submit(){
       $('#form').submit();
}

Edited: Maybe I am not clearly saying my problem. The problem is that when
I click the button, it just submit the form instantly, not waiting me for selected file. I want it to wait me until I selected the file then submit.

I am trying to implement upload file , I want to call the file input to select the file first, after selected the file , then will do form submit. However in my code , it will call form submit immediately. I have tried use ajax but not work. Any way I want to do the form submit just after the user have selected the file. But it seems cannot find a way to wait the finishing of the file input click function ( I mean until the user have selected a file)

Here is my html:

 <input type="text" id="body" name="body" >
 <button type="submit"  id="sent_1" value="Send">Submit</button>         
 <input type="file" id="image_file" name="image" size="chars">
 <button  id="send_img_btn" value="image">Image</button></td>

Here is my javascript:

    $('#send_img_btn').click(function(e){
          e.preventDefault();
    $('#image_file').click();
    $('#form').submit();
});

function imageClick(){
       $('#image_file').click();
}

function submit(){
       $('#form').submit();
}

Edited: Maybe I am not clearly saying my problem. The problem is that when
I click the button, it just submit the form instantly, not waiting me for selected file. I want it to wait me until I selected the file then submit.

Share Improve this question edited Oct 20, 2016 at 2:16 want_to_be_calm asked Oct 19, 2016 at 10:19 want_to_be_calmwant_to_be_calm 1,6673 gold badges26 silver badges44 bronze badges 1
  • 1 If you intend to submit your form as soon as user selects a file, then use the input's change event to acplish the same – akgaur Commented Oct 19, 2016 at 10:29
Add a ment  | 

2 Answers 2

Reset to default 5

Add attribute type="button" in <button> tag.

By default, <button> tag acts like <input type="submit"> inside <form> tag.

<button type="button" id="send_img_btn">Image</button>

If you want to submit the form as soon as the file is selected, use change event of that input.

<form action="formActionUrl" name="myForm">
   <input type="file" name="myInput"/>
</form>

In Script

$(":file").change(function(e){
     $("[name=myForm]").trigger('submit');
});
发布评论

评论列表(0)

  1. 暂无评论