2011-03-14 104 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