`
daigong
  • 浏览: 114377 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

Rail3下 ajax提交Form的写法(JQuery)

阅读更多

转: http://www.alfajango.com/blog/rails-3-remote-links-and-forms/

<script type="text/javascript">
    //处理ajax回调函数
    $(function() {
        // ajax:before   // fires before the request starts, provided a URL was provided in href or action
        // ajax:loading  // fires after the AJAX request is created, but before it's sent
        // ajax:success  // fires after a response is received, provided the response was a 200 or 300 HTTP status code
        // ajax:failure  // fires after a response is received, if the response has a 400 or 500 HTTP status code
        // ajax:complete // fires after ajax:success or ajax:failure
        // ajax:after    // fires after the request events are finished
        $('#ajax_form').bind('ajax:success', function(evt, xhr, settings) {
            alert(xhr.username);
            alert(xhr.password);
        });
    });
</script>
<%= form_tag "/layer/ajax_form", :id=>'ajax_form', :remote=>true, :method =>:post do %>
    <%= label_tag :username, '用户名' %>
    <%= text_field_tag :username %>
    <%= label_tag :password, '密码' %>
    <%= password_field_tag :password %>
    <%= submit_tag '提交请求' %>
<% end %>

<h1>改善下</h1>
<h3>
  使用 url_for 查找Url
  <%= url_for(:controller => 'layer', :action => 'ajax_form') %>
</h3>

<%= form_tag url_for(:controller => 'layer', :action => 'ajax_form'), :id=>'ajax_form_2', :remote=>true, :method => :post do %>
    <%= label_tag :username, '用户名' %>
    <%= text_field_tag :username %>
    <%= label_tag :password, '密码' %>
    <%= password_field_tag :password %>
    <%= submit_tag '提交请求' %>
<% end %>

分享到:
评论
2 楼 lolomarx 2011-07-20  
http://www.alfajango.com/blog/rails-3-remote-links-and-forms/

respond_with do |format|
      format.html do
        if request.xhr?
          render :partial => "comments/show", :locals => { :comment => @comment }, :layout => false, :status => :created
        else
          redirect_to @comment
        end
      end
    end

这篇文章是这样处理的,感觉不是很好,
1 楼 lolomarx 2011-07-20  
在 ie6,7,8 下面 xhr好像不行呢, 把 controller的代码也分享一下如何 ? 遇到的问题是如果不支持 xhr 怎么处理

相关推荐

Global site tag (gtag.js) - Google Analytics