2011-03-14 121 views
0

我有一個表單上傳圖像。jQuery ajax提交不起作用

<% remote_form_for(:avatar, :url => { :controller => "avatar", :action => "upload", :id => @tmp_id }, :html => { :method => :post, :id => 'uploadForm', :multipart => true }) do |f| %> 
        <%= f.file_field :image, :class => 'uploader' %> 

我有一些jQuery來提交表單:

$(document).ready(function() { 
$('.uploader').change(function(){ 
jQuery('#uploadForm').ajaxSubmit({ 
    beforeSubmit: function(a,f,o) { 
    o.dataType = "json"; 
    }, 
    complete: function(XMLHttpRequest, textStatus) { 
    $('#user_image').attr('src', XMLHttpRequest.responseText); 
    return false; 
    }, 
}); 
}); 
}); 

我正在呼籲改變,因爲我有一個警告試了一下功能。但是表單不提交。什麼都沒發生。 任何人都可以看到它錯了嗎?

非常感謝。

回答

0

我想通了。函數寫入的方式不能放在另一個表單中。我必須將它移到外面並使用一些CSS來定位文件上傳按鈕。

我偶然發現了一個奇怪的黑客。如果我做了以下工作:不推薦

<% remote_form_for(:avatar, :url => { :controller => "avatar", :action => "upload", :id => @tmp_id }, :html => { :method => :post, :id => 'uploadForm', :multipart => true }) do |f| %> 
    <% end %> 
    <div class="fileinputs"> 
    <% remote_form_for(:avatar, :url => { :controller => "avatar", :action => "upload", :id => @tmp_id }, :html => { :method => :post, :id => 'uploadForm', :multipart => true }) do |f| %> 
         <%= f.file_field :image, :class => 'file' %> 
         <% end %> 
         </div> 

我基本上寫了兩次表格。

0

是否改變這個

jQuery('#uploadForm').ajaxSubmit({ 

這個

$('#uploadForm').ajaxSubmit({ 

工作?

+0

謝謝。我做了更改,但似乎在嘗試提交表單時會掛起。 – chell 2011-03-14 05:46:20