4處理AJAX文件上傳在我HTML視圖代碼,我有兩個不同的形式,但共用同一個ID名稱困惑如何導軌和回形針
<div class="editcompanyprofile_tab" style="display:block;">
<div class="inputfield-container clearfix">
<%= form_for(@company, remote: true) do |f| %>
<div class="inputfield_row clearfix">
<div class="field" style ="width: 125px;">
<%= f.label :name, "Company Name" %>
</div>
<div class="inputfield">
<%= f.text_field :name, :style => "width: 259px;" %>
</div>
</div>
<div class="inputfield_row clearfix">
<div class="field" style ="width: 125px;">
<%= f.label :firstname, "First Name" %>
</div>
<div class="inputfield">
<%= f.text_field :firstname, :style =>"width: 259px;" %>
</div>
</div>
<div class="inputfield_row clearfix">
<div class="field" style ="width: 125px;">
<%= f.label :lastname, "Last Name" %>
</div>
<div class="inputfield">
<%= f.text_field :lastname, :style =>"width: 259px;" %>
</div>
</div>
<div class="inputfield_row clearfix">
<div class="field" style ="width: 125px;">
<%= f.label :title, "Title"%>
</div>
<div class="inputfield">
<%= f.text_field :title, :style =>"width: 259px;"%>
</div>
</div>
<div class="inputfield_row clearfix">
<div class="field" style ="width: 125px;">
<%= f.label :email, "Email" %>
</div>
<div class="inputfield">
<%= f.text_field :email, :style =>"width: 259px;" %>
</div>
</div>
<div class="inputfield_row clearfix">
<div class="field" style ="width: 125px;">
<%= f.label :phonenumber, "Work Number" %>
</div>
<div class="inputfield">
<%= f.text_field :phonenumber, :style =>"width: 259px;" %>
</div>
</div>
<div class="inputfield_row clearfix" >
<div class="field" style="width: 40px;">
<%= f.label :faxnumber, "Fax"%>
</div>
<div class="inputfield" >
<%= f.text_field :faxnumber, :style=>"width: 134px; margin-right: 5px;"%>
</div>
<div class="field" style="width: 60px;">
<%= f.label :mobile, "Mobile"%>
</div>
<div class="inputfield">
<%= f.text_field :mobile, :style=>"width: 129px;"%>
</div>
</div>
<div class="inputfield_row clearfix">
<div class="field" style ="width: 125px;">
<%= f.label :streetaddress, "Street Address"%>
</div>
<div class="inputfield">
<%= f.text_field :streetaddress, :style=>"width: 259px;" %>
</div>
</div>
<div class="inputfield_row clearfix">
<div class="field" style="width: 40px;">
<%= f.label :city, "City" %>
</div>
<div class="inputfield" >
<%= f.text_field :city, :style =>"width: 134px; margin-right:5px;"%>
</div>
<div class="field" style="width: 60px;">
<%= f.label :state, "State"%>
</div>
<div class="inputfield">
<%= f.text_field :state, :style=>"width: 129px;" %>
</div>
</div>
<div class="inputfield_row clearfix">
<div class="inputfield">
<%= f.text_area :notes, :placeholder =>'Notes' %>
</div>
</div>
<div class="inputfield_buttons">
<div class="inputaction_hc savechanges" style="margin-right:0; width:120px;">
<%= f.submit "Save Changes", :name =>"SAVE", :value =>"Save Changes", :style=>"float: left;" %>
</div>
<div class="inputaction_hc cancel">
<input type="button" name="CANCEL" value="Cancel" onclick="hideDiv('editcompanyprofile-menu');" style="float: left;">
</div>
</div>
<% end %>
</div>
</div>
<div class="logodesign_tab" style="display:none;">
<%= image_tag @company.logo.url(:thumb) %>
<%= form_for(@company, :remote=>true, :html => { :multipart => true }) do |form| %>
<%= form.file_field :logo, :class=>"button" %>
<%= form.submit "Submit", :name =>"SAVE", :value =>"Submit" %>
<input type="button" name="CANCEL" value="Cancel" onclick="hideDiv('editcompanyprofile-menu');">
<% end %>
</div>
在我控制器,這個代碼塊將執行更新操作
def update
respond_to do |format|
if @company.update(company_params)
format.html { redirect_to @company, notice: 'Company was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @company.errors, status: :unprocessable_entity }
end
end
end
的JS Ajax代碼
$("form[id^=edit_company]").on("ajax:success", function(e, data, status, xhr){
alert('ajax succeeded!');
window.location.reload(true);
});
在編輯companyprofile_tab div標記,當我編輯公司的任何字段並保存更改時,上面的js ajax代碼被執行並且觸發警報消息。但對於logodesign_tab div標籤,當我上傳標誌圖像並保存更改,相同的Ajax代碼上面並沒有得到執行!因此警報消息沒有彈出。
這實在令人費解,因爲這兩種兩種形式共享相同的ID即ID^= edit_company所以怎麼來的editcompanyprofile_tab的形式作品,但不是爲logondesign的形式????我在這裏錯過了什麼?!?!?
如果切換徽標文件場用簡單的文本字段,它的工作原理? – crispychicken