简体   繁体   English

提交Ajax后重置自定义文件输入

[英]Reset custom file input after ajax submit

I have custom file inputs on a form that submits files through ajax. 我在通过ajax提交文件的表单上有自定义文件输入。 Everything works correctly and the form gets reset. 一切正常,并且表格被重置。 But I have some javascript actions that are supposed to take place after there's a change in the file input but it only works the first time a file is selected and submitted. 但是我有一些JavaScript操作应该在文件输入发生更改后发生,但仅在第一次选择并提交文件后才起作用。

_form.html.erb _form.html.erb

<%= simple_form_for(@medium, :html => { :id => 'media_form', :multipart => true }, remote: true) do |f| %>

    <div class="custom-file-upload fu1_wrap">

        <label for="file-upload" class="cfu_label"><i class="icon-arrow-up icon-white"></i> Browse
          <%= f.input :asset, label: false, error: false, as: :file, :input_html => { id: 'file-upload', class: 'cfu' } %>
        </label>

    </div>

    <%= f.button :submit, 'Upload', :class => "btn btn-warning" %>

<% end %>

file-input.css 文件input.css

.custom-file-upload input[type="file"] {
    /*opacity: 0;
    filter: alpha(opacity=0);*/
    position: absolute;
    left: -9999px;
}
.cfu_label {
  padding: 6px 12px;
  display: inline-block;
  background: #f06048;
  cursor: pointer;
  color: #fff;
  /* font-size: 15px; */
  text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
  border-radius: 5px;
  opacity: .85;
}
.cfu_label:hover {
    opacity: 1;
}

file-input.js 文件input.js

$(document).ready(function() {

    $('#file-upload').change(function() {
        $('i', '.fu1_wrap').removeClass("icon-arrow-up");
        $('i', '.fu1_wrap').addClass("icon-ok");
        $('label[for="file-upload"]', '.fu1_wrap').css("background-color", '#3fa46a');
    });

});

create.js.erb create.js.erb

$('i', '.custom-file-upload').removeClass("icon-ok");
$('i', '.custom-file-upload').addClass("icon-arrow-up");
$('.cfu_label', '.custom-file-upload').css("background-color", '#f06048');
$(".m_list").prepend("<%= escape_javascript(render :partial => @medium, :locals => {:medium => @medium}) %>");
$('#media').modal('hide');
$('#media_form')[0].reset();

As I stated the form submits perfectly fine; 正如我所说,表格提交得很好。 the problem is that if I try to submit another file after the first one the code from my file-input.js doesn't seem to be firing as the button doesn't change again. 问题是,如果我尝试在第一个文件之后提交另一个文件,则file-input.js按钮file-input.js中的代码似乎不会触发,因为按钮没有再次更改。 It only works the first time. 它只能在第一次使用。 What am I missing here? 我在这里想念什么? Thanks in advance. 提前致谢。

You are attaching your event handler on document "ready" event, but this event only fires once - when the page is first fully loaded. 您将事件处理程序附加到文档“就绪”事件上,但是此事件仅在页面首次完全加载时触发一次。 Right code would be something like this: 正确的代码如下所示:

$(document.body).on('change', '#file-upload', function() {
  $('i', '.fu1_wrap').removeClass("icon-arrow-up");
  $('i', '.fu1_wrap').addClass("icon-ok");
  $('label[for="file-upload"]', '.fu1_wrap').css("background-color", '#3fa46a');
});

Here I attached event handler unobtrusively to the body tag. 在这里,我毫不干扰地将事件处理程序附加到body标签上。 see documentation . 请参阅文档

Edit: to give you more insight on what is happening - you are attaching event handler to your input DOM node, but after you re-render rails view via ajax, that node is gone, rails UJS driver removes it with the rest of the page before inserting re-rendered view, and so event handler is gone too. 编辑:让您对正在发生的事情有更多了解-您正在将事件处理程序附加到输入DOM节点,但是在通过ajax重新渲染Rails视图之后,该节点消失了,Rails UJS驱动程序将其与页面的其余部分一起删除在插入重新渲染的视图之前,事件处理程序也消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM