Home  >  Q&A  >  body text

java - jquery.form.js 插件结合 input file 的onchange 事件实现自动提交 ?

先看表单

 <form name="formObject" role="form" method="post" enctype="multipart/form-data">
  <input class="" type="file" name="personfile" onchange="fun()"/>
  <input class="btn btn-primary" value="提交" type="submit" />
 </form>
 

上面的form 表单利用 jquery.form.js 插件能实现异步 上传文件 , 不过为了用户体验度友好,我想 去掉 input submit 按钮 , 在用户选择图片后 ,利用 input file 的onchange事件实现 jquery.form.js 自动异步提交 , 结果尝试过后还是没有实现,来此求助 !

希望大神给予帮助 !

巴扎黑巴扎黑2717 days ago413

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 14:55:41

     <form id="form" name="formObject" role="form" method="post" enctype="multipart/form-data">
      <input id="file" class="" type="file" name="personfile" />
      <input class="btn btn-primary" value="提交" type="submit" />
     </form>
    $(function() {
        $("#file").on("change", function() {
            $("#form").submit();
        });
    });

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:55:41

    I happened to use this before:

    $(document).ready(function() {
                var options = {
                    dataType: 'json'
                };
                $('#file').on('change', function(){
                    $('#form').ajaxForm(options).submit();
                });
            });

    For the HTML part, please refer to @bordertownmadman’s answer. It’s similar to the one above, but if you are using this http://malsup.com/jquery/form/ plug-in, you can try the one above.

    Written before, the backend uses laravel implementation: http://segmentfault.com/a/1190000002962210

    reply
    0
  • Cancelreply