RoR:提交表单上的jquery文件上传按钮

时间:2015-08-04 16:30:08

标签: javascript jquery ruby-on-rails ruby-on-rails-4 jquery-file-upload

如何制作,以便我的提交按钮上传图片?

<%= simple_form_for @project, html: { multipart: true, id: 'fileupload' } do |f| %>

  <span class="btn btn-success fileinput-button">
    <i class="glyphicon glyphicon-plus"></i>
    <span>Add files...</span>
    <input type="file" name="photos[]" id='photo_upload_btn', multiple>
  </span>
  <button type="submit" class="btn btn-primary start">
    <i class="glyphicon glyphicon-upload"></i>
    <span>Start upload</span>
  </button>
  <button type="reset" class="btn btn-warning cancel">
    <i class="glyphicon glyphicon-ban-circle"></i>
    <span>Cancel upload</span>
  </button>

  <%= f.button :submit, class: "btn btn-primary pull-right" %>
<% end %>


<script>
$(function () {

'use strict'; //not even sure what this is for

$('#fileupload').fileupload({

});
    // Load existing files:
    $('#fileupload').addClass('fileupload-processing');
    $.ajax({
        url: $('#fileupload').fileupload('option', 'url'),
        dataType: 'json',
        context: $('#fileupload')[0]
    }).always(function () {
        $(this).removeClass('fileupload-processing');
    }).done(function (result) {
        $(this).fileupload('option', 'done')
            .call(this, $.Event('done'), {result: result});
    });
});
</script>

现在,当我上传图片时,它会显示缩略图预览和带有取消按钮的开始按钮。我想移动开始按钮并使用提交按钮全部上传。

1 个答案:

答案 0 :(得分:0)

尝试以下示例 -

$('#fileupload').fileupload({
    dataType: 'json',
    add: function (e, data) {            
        $(".btn-primary").off('click').on('click', function () {
            data.submit();
        });
    },
});
相关问题